Php isset($#会话[';用户id';])与登录
我刚刚完成了我的第一个工作登录表单。我可以验证我是否使用特定用户名/ID登录,或者根本没有登录 但是,登录页面无法正常工作。我相信下面的代码应该显示一个登录表单(如果用户尚未登录)或一个到注销页面的链接(如果您已经登录) 如果我把这个贴在页面顶部Php isset($#会话[';用户id';])与登录,php,session,login,Php,Session,Login,我刚刚完成了我的第一个工作登录表单。我可以验证我是否使用特定用户名/ID登录,或者根本没有登录 但是,登录页面无法正常工作。我相信下面的代码应该显示一个登录表单(如果用户尚未登录)或一个到注销页面的链接(如果您已经登录) 如果我把这个贴在页面顶部 session_start(); …然后它反过来工作。如果我已经登录,它会显示一个登录表单,当我已经注销时,它会显示一个注销链接 下面是login_submit.php中的(经过大量编辑的)代码 <?php session_start();
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>
即使我已登录,它也会显示“尚未登录”。在使用$\u会话之前,请始终先使用SESSION\u start。否则,$\u会话将无法工作
<?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; ?>
在这里登录
(等等)
在这里注销
如果我把这个贴在页面顶部。。。会话_start()。。。然后它
工作
你回答了你自己的问题
…相反。如果我已经登录,它会显示一个登录表单,当我已经注销时,它会显示一个注销链接
会话名称为
user\u id
无效,因此将永远不会匹配。每个新页面都是如此。一个新页面,在你提供之前没有任何信息,通常带有变量。
要向其提供有关当前用户的信息(一次可能有多个用户),请使用会话id代码。为此,必须首先启动一个会话。这确保每个用户都启动了自己的会话,并通过自己的会话id信息进行标识。你不想让他们互相获取数据,是吗 使用
\u SESSION
数组的每个页面必须具有SESSION\u start()代码>位于页面的最顶部。如果它的工作方式与您预期的相反,那么您的If语句就会出现一些问题,或者出现未修补的错误。尝试将此行if($user\u id==false)
更改为if($user\u id==false)
这只是一个猜测;首先,什么是“第一”?用户访问的第一个页面是我的主页?我把session_start()放在这里正确吗;在我的1)注册页面、2)登录页面和3)登录_submit.php,然后使用session();在我的网站上几乎每一个页面上?
<?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>
if(isset($_SESSION['valid'])) { echo 'logged in'; } else { echo 'not logged yet'; }
<?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; ?>
<?php if(!isset( $_SESSION['user_id'] ) ): ?>
^-------------------Here
if(isset($_SESSION['valid'])) { echo 'logged in'; } else { echo 'not logged yet'; }
^------- here