我是一个PHP的新手,尝试编写自己的登录代码时遇到了些问题。我的会话(session)不能保留,如果我进入其他页面,我的网站就无法记住我已经登录。
<?php
$logincontent;
require_once 'config.php';
$logincontent=
' <div class="column-1-3">
<div class="white-box">
<div class="box-title">
Login
<div class="subtitle"><img src="images/subtitle-lets-work-together.png" alt="let\'s work together" /></div>
<div class="icon"><img src="images/title-icon-contact.png" alt="" /></div>
</div>
<div class="box-content fixed-height">
<form action="" method="post" class="contact-form">
<div>
<b>Username</b>
<input class="text-input" type="text" name="username" value="" onfocus="clearInput(this);" onblur="restoreInput(this);" /><br/>
<b>Password</b>
<input class="text-input" type="text" name="password" value="" onfocus="clearInput(this);" onblur="restoreInput(this);" /><br/>
<div class="align-right">
<span class="blue-button"><span><input type="submit" value="SEND »" /></span></span>
</div>
<input type="hidden" name="val" value="checkin">
</div>
</form>
</div>
</div>
</div><!--/end .column-1-3 --> ';
if (isset($_POST["val"])&&($_POST["val"]=='checkin'))
{
echo "checkin";
DBConnect();
$username=$_POST['username'];
$password=$_POST['password'];
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);
$sql="SELECT * FROM users WHERE username='$username' and password='$password'";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1)
{
@session_start();
// Register $myusername, $mypassword and redirect to file "login_success.php"
@session_register('username');
@session_register('password');
$_SESSION['username'] = $username;
$_SESSION['password'] = $password;
}
else
{
echo "Wrong Username or Password";
unset($username);
}
} else
if ((isset($_POST["val"]))&&($_POST["val"]=='logout'))
{
@session_start();
session_destroy();
} else
{
//echo $logincontent;
}
//if((isset($username))&&(!@session_is_registered($username)))
@session_start();
//if((@session_is_registered('username')))
if (isset($_SESSION['username']) && isset($_SESSION['password']))
{
if (isset($db_conn) == false)
DBConnect();
$username=$_SESSION['username'];
$password=$_SESSION['password'];
//echo $username;
//echo $password;
$sql="SELECT * FROM users WHERE username='$username' and password='$password'";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
$logincontent=
' <div class="column-1-3">
<div class="white-box">
<div class="box-title">
Welcome back!
<div class="subtitle"><img src="images/subtitle-lets-work-together.png" alt="let\'s work together" /></div>
<div class="icon"><img src="images/title-icon-contact.png" alt="" /></div>
</div>
<div class="box-content fixed-height">
<form action="" method="post" class="contact-form">
<div>
Username: <b>'.$row['username'].'</b><br><br>
Name: <b>'.$row['name'].'</b><br>';
//if (mysql_field_len ($row['avatar'])==0)
if (isset($row['avatar']))
{
$Link='images/no_avatar.gif';
} else
{
$Link=$row['avatar'];
}
$logincontent=$logincontent."<center>
<div class='avatar-image'><img src='".$Link."' alt='' width='100' height='100'/></div></center><br>".'
<div class="align-center">
<span class="blue-button"><span><input type="submit" value="logout" /></span></span>
</div>
<input type="hidden" name="val" value="logout">'."
</div>
</form>
</div>
</div>
</div><!--/end .column-1-3 --> ";
}
?>
session_start();
吗?我猜想你发布的这个文件是被包含到其他文件中的。你需要在每个文件中添加session_start();
。如果你只有一个 index 文件,那么只需要在 index 中添加。 - DeiFormsession_start();
。在doctype或html标签之前。 - DeiForm