isset ( $_SESSION['user_id'] ) 与登录
isset ( $_SESSION['user_id'] ) vs Login
我刚刚完成了我的第一个工作登录表单。我可以验证我是使用特定 username/ID 登录还是根本没有登录。
但是,登录页面无法正常工作。我相信下面的代码应该显示登录表单(如果用户尚未登录)或 link 到注销页面(如果您已经登录)...
<?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; ?>
但它只显示注销 link,即使我已登录。
我不知道从哪里开始解决这个问题,因为这对我来说都很陌生。我有一个数据库 table,其中包含字段 'user_id'(以及 'username' 和 'password'。在本例中,user_id 的值是数字 1。
我尝试回显 user_id 的值但没有成功,即使已登录...
echo $_SESSION['user_id'];
如果我将其粘贴到页面顶部...
session_start();
...然后它反过来工作。如果我已登录,它会显示一个登录表单,并在我已经注销时显示一个注销 link。
这是来自 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>
P.S。从@ 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'; }
即使我已登录,它仍然显示 "not logged yet"。
在使用 $_SESSION 之前,请始终先使用 session_start。否则,$_SESSION 将无法工作。
<?php session_start();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; ?>
If I paste this at the top of the page... session_start(); ...then it
works ...
您回答了自己的问题。
..in reverse. If I'm logged in, it displays a login form, and it displays a logout link when I'm already logged out.
<?php if(!isset( $_SESSION['user_id'] ) ): ?>
^-------------------Here
你只是忘了否定 if 语句
at the top of the page, then added this script:
if(isset($_SESSION['valid'])) { echo 'logged in'; } else { echo 'not logged yet'; }
^------- here
会话名称是 user_id
而不是 valid
因此永远不会匹配。
每个新页面都是这样。一个新页面,在您提供之前没有任何信息,通常带有变量。
要向其提供有关当前用户(可能同时有多个用户)的信息,请使用 session_id 代码。为此,您必须首先开始一个会话。这确保每个用户都有自己的会话开始,由他们自己的 session_id 信息识别。你不希望他们都得到彼此的数据,是吗?
我刚刚完成了我的第一个工作登录表单。我可以验证我是使用特定 username/ID 登录还是根本没有登录。
但是,登录页面无法正常工作。我相信下面的代码应该显示登录表单(如果用户尚未登录)或 link 到注销页面(如果您已经登录)...
<?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; ?>
但它只显示注销 link,即使我已登录。
我不知道从哪里开始解决这个问题,因为这对我来说都很陌生。我有一个数据库 table,其中包含字段 'user_id'(以及 'username' 和 'password'。在本例中,user_id 的值是数字 1。
我尝试回显 user_id 的值但没有成功,即使已登录...
echo $_SESSION['user_id'];
如果我将其粘贴到页面顶部...
session_start();
...然后它反过来工作。如果我已登录,它会显示一个登录表单,并在我已经注销时显示一个注销 link。
这是来自 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>
P.S。从@ 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'; }
即使我已登录,它仍然显示 "not logged yet"。
在使用 $_SESSION 之前,请始终先使用 session_start。否则,$_SESSION 将无法工作。
<?php session_start();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; ?>
If I paste this at the top of the page... session_start(); ...then it works ...
您回答了自己的问题。
..in reverse. If I'm logged in, it displays a login form, and it displays a logout link when I'm already logged out.
<?php if(!isset( $_SESSION['user_id'] ) ): ?>
^-------------------Here
你只是忘了否定 if 语句
at the top of the page, then added this script:
if(isset($_SESSION['valid'])) { echo 'logged in'; } else { echo 'not logged yet'; }
^------- here
会话名称是 user_id
而不是 valid
因此永远不会匹配。
每个新页面都是这样。一个新页面,在您提供之前没有任何信息,通常带有变量。 要向其提供有关当前用户(可能同时有多个用户)的信息,请使用 session_id 代码。为此,您必须首先开始一个会话。这确保每个用户都有自己的会话开始,由他们自己的 session_id 信息识别。你不希望他们都得到彼此的数据,是吗?