[英]Using PHP sessions after login
好的,所以我是PHP新手。 使用会话时遇到问题。 我创建了一个小型的预订系统,用户可以在其中注册,登录和预订活动。 除了会议以外的基本内容。 我遇到的问题是,一旦用户注册然后登录,如果他们想从该页面更改用户名,就可以。 但是,如果用户确实更改了用户名,则他们将无法重新登录系统。 请帮助。
login.php页面
<?php
session_start();
$con = mysql_connect("localhost","root","");
if(!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("flexiflash", $con);
$username = strtolower($_POST['user']);
$password = strtolower($_POST['pass']);
$result = mysql_query("SELECT * FROM user WHERE Username = '$username' AND Password = '$password'");
$usertype = mysql_query("SELECT Usertype FROM user");
if(($row = mysql_fetch_array($result)))
{
$usertype = $row['Usertype'];
//if an admin user, redirect to admin.html
if ($usertype == 'Admin')
{
//Creating the session
$_SESSION['usertype'] = 'admin';
$_SESSION['userid'] = $row['User_ID'];
header('Location: admin.php');
}
if ($usertype == 'User')
{
$_SESSION['usertype'] = 'user';
// more??
header('Location: user.php');
}
}
else
{
echo "<h2>Why do I see this page?</h2>";
echo "<p>Your username may have not been found</p>";
echo "<p>Your password was entered incorrectly</p>";
echo "<h2>What can I do?</h2>";
echo "<p>double check you have entered your username and password correctly</p>";
echo "<br/>";
echo "<a href='index.html' alt='click to go back home'>Click here to go back to login page!</a>";
}
mysql_close($con);
?>
update_user.php页面
<?php
session_start();
$con = mysql_connect("localhost","root","");
if(!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("flexiflash", $con);
$username = $_POST['U_user1'];
$username_confirm = $_POST['U_user2'];
$id = $_SESSION['userid'];
if ($username == $username_confirm)
{
$result = mysql_query("UPDATE user SET Username = ' " . $username . " ' WHERE Username = '$id'");
echo "Record Updated Succesfully";
}
else
{
echo "<h2>Why do I see this page?</h2>";
echo "<p>Your username may have not been found</p>";
echo "<h2>What can I do?</h2>";
echo "<p>double check you have entered your username correctly</p>";
}
mysql_close($con);
?>
先谢谢您的帮助 :)
在您的更新查询中,您在用户名周围使用空格:
"UPDATE user SET Username = ' " . $username . " '...
因此,用户名“ foo”变为“ foo”。
我认为这部分也是错误的:
WHERE Username = '$id'
看起来应该是:
WHERE User_ID = '$id'
警告 :您的代码存在一些严重的安全性问题,现在您很容易受到SQL注入攻击的影响。 阅读正确过滤用户输入的内容。
另外,您正在使用不mysql_*
使用的mysql_*
方法,应切换到mysqli_*
或PDO
。 然后使用带有绑定参数的准备好的语句!
您的UPDATE语句需要为:
"UPDATE user SET Username = '$username' WHERE User_id = '$id'"
您不需要在'$ username'周围加上引号; 以双引号开头的字符串意味着该字符串将这样识别变量。 但是最重要的部分是您需要通过ID而不是用户名来识别用户。 如果允许最终用户更新其用户名,则需要使用其他变量作为唯一标识符。 这样,即使它们的用户名更改了,您也总是知道数据库中的哪个记录属于它们。
还有一点需要注意的是-如果用户不是管理员,就好像您没有为user_id设置$ _SESSION变量。 您也需要这样做。
更新用户名后,还使用此新用户名更新会话,然后它将正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.