会话未被存储,除非我销毁非存在的会话。我正在尝试创建登录表单,每次登录时它都不起作用,除非我去logout.php,它有session_destroy();然后,当我再次登录时,它完美无缺。
代码:
<?php
@session_start();
require_once ( 'include/config.php' );
class users{
public function login() {
session_start();
if(!isset($_SESSION['id'])) {
$login_submit=$_POST ['submit'];
if (isset($login_submit)) {
$login_username=$_POST ['username'];
$login_password=$_POST ['password'];
$login_username = stripslashes($login_username);
login_password = stripslashes($login_password);
$login_username = mysql_real_escape_string($login_username);
$login_password = mysql_real_escape_string($login_password);
$site_info=mysql_query("SELECT * FROM site_settings");
$site_settings=mysql_fetch_object($site_info);
$site_url=$site_settings->site_url;
$login=mysql_query("SELECT * FROM users where username='$login_username' AND password='$login_password' ");
$rows=mysql_num_rows($login);
$admin_login=mysql_query("SELECT * FROM admin where admin_u='$login_username' AND admin_p='$login_password' ");
$admin_login_info=mysql_fetch_object($admin_login);
$admin_rows=mysql_num_rows($admin_login);
if($rows == 1) {
$linfo=mysql_fetch_object($login);
$_SESSION ['id']=$linfo->id;
$_SESSION ['username']=$login_username;
echo "<meta http-equiv='refresh' content='0,overview'>";
}
elseif($admin_rows == 1) {
$_SESSION ['adminid']=$admin_login_info->id;
$_SESSION ['adminusername']=$admin_login_info->username;
echo "<meta http-equiv='refresh' content='0,admin/services.php'>";
}
else {
echo "<p id='lger'> اسم المستخدم او كلمة السر غير صحيحة </p>";
}
}
}else {echo "<meta http-equiv='refresh' content='0,overview'>";}
}
}
?>
页面代码包含登录功能:
<?php
@session_start();
require_once ( 'include/functions.php' );
if(!isset($_SESSION['id'])) {
print ("
<div id='login'>
<p id='loginp'>تسجيل الدخول </p><br><br>
<form action='index' method='post' id='form'>
<input type='text' name='username' placeholder='اسم المستخدم' class='linput'> </input><img src='images/login.png' class='limg'></img><br><br>
<input type='password' name='password' placeholder='كلمة السر' class='linput'></input><img src='images/lock.png' class='limg'></img><br><br>
<input type='submit' name='submit' value='دخول' class='lbutton'> </input>
</form>
<br>
<hr id='hrlogin'>
<div id='aga'><p id='new'>لاتمتلك حساب؟</p>
<a href='signup.php' id='nhref'>أنشئ حساب جديد</a></div>
</div>
");
$users = new users();
$users->login();
} else {
echo "<meta http-equiv='refresh' content='0,overview'>";
}
?>
答案 0 :(得分:1)
您可以随时使用以下内容测试您的会话:
if(! isset($_SESSION)) session_start();
除了上述内容之外,如果您想考虑检查是否由于某种原因禁用了会话,您可以抑制错误消息,设置测试会话变量,然后验证它是否已设置。如果不是,您可以为禁用的会话编写代码。您希望在脚本开头或附近完成所有这些操作。所以作为一个例子(根据错误处理设置的不同而不同):
$_SESSION['valid'] = 'valid';
if('valid' !== $_SESSION['valid'])
{
// handle disabled sessions
}
如果您使用的是PHP 5.4+,还有一个名为session_status()
if (PHP_SESSION_NONE === session_status()) session_start();
希望这有帮助。