doukanxi4246 2013-12-25 12:24
浏览 31

需要帮助重写脚本[关闭]

I made a PHP login script but the best I came up with wasn't quite what I expected. It is a emergency solution to have at least a very simple login, but I wanted to get it a little nicer.

The script redirects all people to one single same page. Now I need this script to be rewritten so that it redirects user to their own private page. Please note that I want the script not too be much more complicated or different.

Here is the script I have so far:

<?php
    $user_pass_list = array ("user1" => "pass1", "user2" => "pass2");
    $user = $_POST['user'];
    $pass = $_POST['pass'];
    if(array_key_exists($user,$user_pass_list)){
        if($user_pass_list[$user] == $pass){
            header("location: http://www.examplesite.net");
        } else{
          echo "login failed";
        }
    }
?>

As you can see it always direct to examplesite.net, for every user in the array. Now I want the script to redirect an account to its personal page, so that I can write different info on the page, etc.

Can somebody do this?

  • 写回答

1条回答 默认 最新

  • douchuilai2355 2013-12-25 12:38
    关注

    All you would really have to do is append the user to the URL you are redirecting to.

    header( "location: http://www.examplesite.net/" . $user );
    

    This is the simplest way but it has a problem. This will mean that your some other URL's might be taken by users! What if a person's user name is "FAQ". That would mean that his user page would be:

    http://www.examplesite.net/faq
    

    What happens if the user chooses a name like "about" or "help". I think you see where I'm going with this.

    To avoid this problem, you could redirect the users to a page like this:

    header( "location: http://www.examplesite.net/user/" . $user );
    

    Now you can have a separate script to handle user pages. We're on the right track!

    In order to manage these URLs, I'd recommend using an .htaccess file to re-route requests to the correct page. Something like this:

    RewriteCond $1 ^users/ # Matches URLs starting with users
    RewriteCond %{REQUEST_FILENAME} !-f # skip direct requests for files
    RewriteCond %{REQUEST_FILENAME} !-d # skip direct requests for directories
    RewriteRule ^(.*)$ user_page.php?username=$1 [L] # redirect with GET param
    

    With these lines in your htaccess, any request to http://www.examplesite.net/user/ will be redirected to the user_page.php script in the root directory of the site. That page will receive a get parameter called $_GET[ "username" ] which will be the users name.

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大