Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
登录后重定向每个用户的php脚本_Php_Login - Fatal编程技术网

登录后重定向每个用户的php脚本

登录后重定向每个用户的php脚本,php,login,Php,Login,我已经设置了一个php脚本,在登录后将用户重定向到特定的网页/php页面。但是我想把每个用户引导到不同的页面 例如,每个登录的客户都有自己独特的主页 如果我的checklogin.php中有这个部分,我假设它与此有关。我最近才开始学习php。多谢各位 <?php session_start(); $username = mysql_real_escape_string($_POST['username']); $password = mysql_real_escap

我已经设置了一个php脚本,在登录后将用户重定向到特定的网页/php页面。但是我想把每个用户引导到不同的页面

例如,每个登录的客户都有自己独特的主页

如果我的checklogin.php中有这个部分,我假设它与此有关。我最近才开始学习php。多谢各位

<?php
    session_start();
    $username = mysql_real_escape_string($_POST['username']);
    $password = mysql_real_escape_string($_POST['password']);
    mysql_connect("localhost", "root","root") or die(mysql_error()); //Connect to server
    mysql_select_db("first_db") or die("Cannot connect to database"); //Connect to database
    $query = mysql_query("SELECT * from users WHERE username='$username'"); //Query the users table if there are matching rows equal to $username
    $exists = mysql_num_rows($query); //Checks if username exists
    $table_users = "";
    $table_password = "";
    if($exists > 0) //IF there are no returning rows or no existing username
    {
        while($row = mysql_fetch_assoc($query)) //display all rows from query
        {
            $table_users = $row['username']; // the first username row is passed on to $table_users, and so on until the query is finished
            $table_password = $row['password']; // the first password row is passed on to $table_users, and so on until the query is finished
        }
        if(($username == $table_users) && ($password == $table_password)) // checks if there are any matching fields
        {
                if($password == $table_password)
                {
                    $_SESSION['user'] = $username; //set the username in a session. This serves as a global variable
                    header("location: home.php"); // redirects the user to the authenticated home page
                }

        }
        else
        {
            Print '<script>alert("Incorrect Password!");</script>'; //Prompts the user
            Print '<script>window.location.assign("login.php");</script>'; // redirects to login.php
        }
    }
    else
    {
        Print '<script>alert("Incorrect Username!");</script>'; //Prompts the user
        Print '<script>window.location.assign("login.php");</script>'; // redirects to login.php
    }
?>


您可以在数据库中添加一列:“level”并执行一些IF,您可以将它们重定向到所需的页面

编辑:

在以下位置编辑代码:

 if($password == $table_password)
                {
                    $_SESSION['user'] = $username; //set the username in a session. This serves as a global variable
                    header("location: home.php"); // redirects the user to the authenticated home page
                }
改为:

 if($password == $table_password)
                {
                    $_SESSION['user'] = $username; //set the username in a session. This serves as a global variable
                    header("location: home.php?user=".$username); // redirects the user to the authenticated home page
                }
在home.php上写:

<?php

$username = $_GET['user'];

echo "I'm on page home with user:" , $username;

?>

如果要将用户动态重定向到另一个网页,请使用javascript。PHP“仅”发送特定文件的内容。

您也可以对所有用户使用相同的页面,但更改其内容。我能想到的最简单的情况是,删除-say-user.php的所有内容,然后从数据库或根据用户的不同从另一个文件加载html。

我假设您想在数据库中显示有关它们的信息

现在,您可以使用帐户的唯一ID(也应该存储在数据库中)将它们指向url,例如:index.php?ID=$account\u ID

从url获取ID(有点像:
$\u Get['ID']
)并运行查询,例如:
select*from profile,其中ID=$ID

注意{

我在这里使用变量
$id
,而不是
$\u GET['id']
,因为可能的错误

}

之后,您将在页面上显示帐户配置文件信息,由于
&id=$account\u id
(其中$account\u id是登录人员的id),因此这是他们自己的页面

编辑:

要将它们重定向到自己的页面,您必须通过查询从数据库中获取ID:

select id from accounts where username = $username;
如果用户名是唯一的,则此选项有效

您可以自己在单独的变量中获取id

之后,您可以使用ID将他们引导到自己的页面,如下所示:

header("location: home.php?id=".$id);
EDIT2:(根据评论)

每次都会将您重定向到home.php。唯一的区别是,
?id=
部分对于每个用户都是不同的。现在打开home.php:“


这意味着您应该将每个用户的配置文件首选项存储在数据库中,并在主页上再次获取它。

我认为您可以将用户重定向到同一页面(ThisUserProfile或类似的smth),但根据用户id生成此页面的内容。但这意味着您需要将更多信息添加到存储用户信息的表中。

我目前正在使用MAMP,并将用户详细信息存储在数据库中。目前唯一的登录详细信息是用户名和密码。您所说的“级别”是什么意思?使用level,我的意思是按节共享用户。您可以检查user=“customer1”的节是否正确,然后将其重定向到database1.php elseif user=“customer2”的节“…啊,这是有道理的,谢谢,但我正在努力找出如何重定向它们。目前,一旦验证了密码和用户名,它们都会被重定向到home.php页面,这会将它们带到一个特定的网页,我有4个单独的php文件,我需要将我的4个用户定向到他们自己的唯一页面。谢谢。哦。。我知道你想要什么。。我想是动态网页。你应该只有一个像“user.php”这样的php,看看我的pastebin:,你会注意到重定向时的标题会有你的用户名。看了几个论坛后,我认为你有一个动态网页是对的。我已经修改了本页顶部的问题,并插入了checklogin.php。目前我没有user.php,我相信我已经在checklogin.php中包含了其中的一部分。或者我还需要创建user.php吗?因为我需要一个地方告诉程序把每个用户发送到哪里?谢谢你的回复。每个用户都有一个包含其公司唯一数据库的页面。我正在使用MAMP,客户的登录详细信息是“用户名”和“密码”。我想用下面的方法将客户重定向到database1.php,customer2重定向到database2.php。感谢这一点,大多数都是有意义的(仍在学习如何使用php以及如何为MAMP组合所有内容)。每个用户都有一个数字表,只有他们的公司才能看到。每个表都存储在一个单独的php文件中。因此,当他们登录时,我希望他们被定向到自己的php文件。我已经设置了检查用户名和密码的查询,但目前所有用户都指向home.php文件。然后在home.php文件中重定向用户吗?你能给我一个如何重定向它们的例子吗,代码。非常感谢。@Rus84当然,但首先,您的数据库中是否有主键(我想您有)如果有,什么是主键?User_ID或Username??主键是ID(其他两列是Username和password),谢谢您的提醒。我在顶部修改了我的问题,并添加了checklogin.php,这显然会检查我的用户名和密码是否与数据库中存储的用户名和密码匹配。然后我是否也需要添加SELECTFROM id行?真是愚蠢的问题。。。。。我在什么时候将用户与我希望他们重定向到的页面进行匹配?我必须在home.php中写入该位吗?为你的生日干杯help@Rus84您已经有了这一行:
$query=mysql\u query(“从username='$username'的用户中选择*)
所以现在您可以从中获得
行['id']
,并在
标题中使用它(“location:home.php?id=“.id$id”)。你不必做任何其他事情。您可以在下一页使用
$\u GET['id']
,然后在另一个查询中使用它。
    while($row = mysql_fetch_assoc($query)) //display all rows from query
    {
        $table_users = $row['username']; // the first username row is passed on to $table_users, and so on until the query is finished
        $table_password = $row['password']; // the first password row is passed on to $table_users, and so on until the query is finished
        $table_id = $row['id'];

    }
    if(($username == $table_users) && ($password == $table_password)) // checks if there are any matching fields
    {
            if($password == $table_password)
            {
                $_SESSION['user'] = $username; //set the username in a session. This serves as a global variable
                header("location: home.php?id=".$table_id); // redirects the user to the authenticated home page
            }

    }
$id=$_GET['id'];
//Some mysql injection prevention.
$query = mysql_query("SELECT * from profile WHERE id='$id'");