我是有一天PHP的初學者。我試圖創建我的PHP登錄代碼,我有一些問題。會議不會保留,如果我去其他網頁上我的網站不記得我登錄。PHP會話登錄
<?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))&&([email protected]_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 --> ";
}
?>
它仍然不起作用http://pastebin.com/R3XmW6wu – user558126
你把'session_start();'放在每個文件的開始處嗎?我想這張你發佈的文件將它包含在其他文件中。您需要爲每個文件添加'session_start();'。如果你只有索引,所以只能索引 – DeiForm
這個腳本是在login.php中,我有index.php,template.php和about.php。我需要把session_start();在他們所有? – user558126