我刚刚完成了我的第一个工作登录表单。我可以验证我是否使用特定的用户名/ ID登录或者根本没有登录。
但是,登录页面无法正常工作。我相信下面的代码应该显示登录表单(如果用户尚未登录)或指向登出页面的链接(如果您已经登录)...
<?php if( isset( $_SESSION['user_id'] ) ): ?>
<h2>Login Here</h2>
<form action="login-submit.php" method="post">
(etc.)
<?php else: ?>
<h2>Logout Here</h2>
<p><a href="logout.php">Log Out Link</a></p>
<?php endif; ?>
但它只显示注销链接,即使我已登录。
我不知道从哪里开始对此进行故障排除,因为对我来说这一切都不熟悉。我有一个带有'user_id'字段的数据库表(以及'username'和'password'。在这个例子中,user_id的值是数字1。
我试图回复user_id的值而没有成功,即使登录时...
echo $_SESSION['user_id'];
如果我将其粘贴在页面顶部...
session_start();
...然后它反向运作。如果我已登录,则会显示登录表单,并在我已注销时显示注销链接。
这是来自login_submit.php的(经过大量编辑的)代码...
<?php
session_start();
/*** check if the users is already logged in ***/
if(isset( $_SESSION['user_id'] ))
{
$message = 'Users is already logged in';
}
/*** check that both the username, password have been submitted ***/
if(!isset( $_POST['username']))
{
$message = 'Please enter a valid username and password';
}
else
{
/*** if we are here the data is valid and we can insert it into database ***/
$username = filter_var($_POST['username'], FILTER_SANITIZE_STRING);
$password = filter_var($_POST['password'], FILTER_SANITIZE_STRING);
/*** now we can encrypt the password ***/
$password = sha1( $password );
/*** connect to database ***/
try
{
// Database stuff...
/*** prepare the select statement ***/
$stmt = $dbh->prepare("SELECT user_id, username, password FROM g1_members WHERE username = :username AND password = :password");
/*** bind the parameters ***/
$stmt->bindParam(':username', $username, PDO::PARAM_STR);
$stmt->bindParam(':password', $password, PDO::PARAM_STR, 40);
/*** execute the prepared statement ***/
$stmt->execute();
/*** check for a result ***/
$user_id = $stmt->fetchColumn();
/*** if we have no result then fail boat ***/
if($user_id == false)
{
$message = 'Login Failed';
}
/*** if we do have a result, all is well ***/
else
{
/*** set the session user_id variable ***/
$_SESSION['user_id'] = $user_id;
/*** tell the user we are logged in ***/
$message = 'You are now logged in';
}
}
catch(Exception $e)
{
/*** if we are here, something has gone wrong with the database ***/
$message = 'We are unable to process your request. Please try again later.';
}
}
?>
<html>
<head>
<title>PHPRO Login</title>
</head>
<body>
<p><?php echo $message; ?>
</body>
</html>
附:从讨论@ isset() didn't work after login with $_SESSION set to valid中得到一个提示我插入了session_start();在页面顶部,然后添加此脚本:
if(isset($_SESSION['valid'])) { echo 'logged in'; } else { echo 'not logged yet'; }
它甚至在我登录时都说“尚未登录”。
如果我将其粘贴在页面顶部... session_start(); ......那么它有效......
你是在自问自答。
..相反。如果我已登录,则会显示登录表单,并在我已注销时显示注销链接。
<?php if(!isset( $_SESSION['user_id'] ) ): ?>
^-------------------Here
你只是忘了否定if语句
在页面顶部,然后添加此脚本:
if(isset($_SESSION['valid'])) { echo 'logged in'; } else { echo 'not logged yet'; }
^------- here
会话名称是qazxsw poi而不是qazxsw poi这将永远不会匹配。
在使用$ _SESSION之前,请先使用session_start。否则,$ _SESSION将无效。
user_id
每个新页面就是这样。一个新页面,在您提供之前没有任何信息,通常包含变量。要向其提供有关当前用户的信息(并且可能同时存在多个用户),请使用session_id代码。要做到这一点,您必须首先开始一个会话。这确保每个用户都有自己的会话启动,由他们自己的session_id信息识别。你不希望他们都得到彼此的数据,对吗?