我已经设置了一个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
}
?>
答案 0 :(得分:1)
您可以在数据库中添加一列:&#34; level&#34;做一些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;
?>
现在你可以做任何你想做的事,你有一个动态的网页,你可以&#34;玩&#34; with database使用WHERE username = $ username
在home.php上创建新列和查询答案 1 :(得分:1)
我假设您要在数据库中显示有关它们的信息。
现在你可以直接将它们引导到具有帐户唯一ID的URL(也应该存储在数据库中),例如:index.php?id = $ account_id,
从网址获取ID(有点像:$_GET['id']
)并运行查询,例如:select * from profile where id=$id
注意{强>
由于可能mysql injection,我在此处使用变量$id
而不是$_GET['id']
。
<强>} 强>
之后,您在页面上显示帐户个人资料信息,并且由于&id=$account_id
(其中$ account_id是登录人员的ID),它是他们自己的页面。
修改强>
要将它们重定向到自己的页面,您必须通过查询从数据库中获取ID:
select id from accounts where username = $username;
如果用户名是唯一的,则此方法有效。
您可以自己在单独的变量中获取ID。
之后,你可以使用ID将它们引导到他们自己的页面,如下所示:
header("location: home.php?id=".$id);
EDIT2 :(根据评论)
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
}
}
每次都会将您重定向到home.php。唯一的区别是,?id=
部分对每个用户都不同。现在在home.php上:&#34;
$id=$_GET['id'];
//Some mysql injection prevention.
$query = mysql_query("SELECT * from profile WHERE id='$id'");
这意味着您应该将每个用户的个人资料首选项存储在数据库中,然后在主页上再次将其取出。
答案 2 :(得分:0)
如果要将用户动态重定向到其他网页,请使用javascript。 PHP“仅”发送特定文件的内容。 您也可以为所有用户使用相同的页面,但更改其内容。我能想到的最简单的情况是删除所有内容 - 比如user.php并从数据库加载html,或根据用户加载另一个文件。
答案 3 :(得分:0)
我认为您可以将用户重定向到同一页面(ThisUserProfile或类似的那样),但会根据用户的ID为该页面生成内容。但这意味着您需要在存储用户信息的表格中添加更多信息。