我通过ajax将表单提交到PHP代码页面,该代码将设置或不设置会话。这就是:
$('#login').load('loginform.php');
)
如果我登录新页面,它可以正常工作。
如果我已登录,则注销此工作并再次显示登录表单。 但是,如果我再次尝试从此处登录此同一页面,则不会设置会话。 我正在用PHP输出我的会话,当我注销时,我有这个:
Notice: Undefined index: log in...
在登录同一页面后尝试登录时我得到了这个:
Notice: Undefined variable: _SESSION in...
有什么想法吗?
修改: 这是一些代码 -
的index.php
<script language="javascript">
$(document).ready(function()
{
$("#login_form").submit(function(event)
{
$.post("login.php",{emai:$('#emai').val(),pass:$('#pass').val()} ,function(data)
{
//if login.php echos "no" then an error box is displayed, else it refreshes the login div to hopefully display a log out link
if(data!='no') {$('#login').load('loginform.php');}
if(data =='no') {$("#loginerror").show();}
});
})
//the session is destroyed in logout.php, then the form reloaded.
$("#logout").click(function()
{
$.post("logout.php");
$('#login').load('loginform.php');});
});
</script>
</head>
<body>
<?
include ('inc/classes.php');
session_start();
?>
<div class="header">
<div id="login"><? include ('inc/headerlogin.php');?></div>
</div>
的login.php
//just searching the database for a match and setting the session. Echos yes or no.
<?
session_start();
include ('../inc/classes.php');
$emai=$_POST['emai'];
$pass=$_POST['pass'];
$Query="SELECT * FROM users WHERE emai ='".$emai."' AND pass='".$pass."'";
$Sel = new Database;
$Sel->nextRecord();
$Sel->Query($Query);
if ($Sel->numRows() == 1) {$_SESSION['log'] = $Sel->Record['id']; $login = "yes";}
else {$login = "no";}
echo $login;
?>
LOGINFORM.PHP
<?
echo $_SESSION['log'];
//if session is set display sign out link
if(isset($_SESSION['log']))
{
?>
<a id="logout">sign out</a>
<? } //else display form again
else
{?>
<div id="loginerror">Wrong password</div>
<form method="post" action="" id="login_form">
<input name="email" type="text" value="" id="emai"/>
<input name="pass" type="password" id="pass"/>
<input name="signin" type="submit" value="sign in" id="signbutton"/>
</form>
<? } ?>
答案 0 :(得分:1)
首先确保你拥有id为'login'的html元素,然后确保你在PHP文件的开头放置一个session_start()函数