重新加载后,页面变为空白

时间:2014-08-05 12:01:47

标签: php mysql session login

在重新加载成员页面后(登录后),该页面变为空白。但所有其他事情都正常运作。

bellow是登录表单页面后的login_execution.php页面的代码

<?php

session_start();
$con=mysql_connect("localhost","root","");
if (!$con) {
    die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db('loged', $con);
if(isset($_POST["submit"])) {
$username=mysql_real_escape_string($_POST['username']); 
$password=mysql_real_escape_string($_POST['password']); 
$password=md5($password); 
 $sql="SELECT username FROM  list WHERE username='$username' and password='$password'";
$result=mysql_query($sql);
$count=mysql_num_rows($result);


if($count==1)
{
session_regenerate_id();
    $_SESSION['username'] = $username;
    $_SESSION['password'] = $password;
    session_write_close();
    echo "<table>
<tr>
<th>username</th>";



     $row = mysql_fetch_array($result) 

    ?>
  <tr style="background-color:blue;color:white;"> 
          <td><font size="6"><?php echo $row['username']; ?></font></td>
          <h1 align="center"><font size="6" color="blue">welcome <?php echo $row['username']; ?></font></h1>
          <p align="right"><font size="4" color="#380B61"><a href="logout.php">Logout</a></font></td>





        </tr>
          <?php


echo "</table>";
}

else 
{
?> 
<h1 align="center"><font size="4" color="red">enter coerrect username and password</font></h1>
<td><a href="login.php">BACK</a></td>
<?php
}
}
?>

2 个答案:

答案 0 :(得分:0)

您的条件是,如果在您重新加载页面时单击了提交,则不会单击提交按钮,因此它不会显示任何内容。

要纠正此问题,请使用以下代码。

<?php

session_start();
$con=mysql_connect("localhost","root","");
if (!$con) {
    die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db('loged', $con);
if(isset($_POST["submit"])) {
$username=mysql_real_escape_string($_POST['username']); 
$password=mysql_real_escape_string($_POST['password']); 
$password=md5($password); 
 $sql="SELECT username FROM  list WHERE username='$username' and password='$password'";
$result=mysql_query($sql);
$count=mysql_num_rows($result);

if($count==1)
{
$row = mysql_fetch_array($result)
session_regenerate_id();
    $_SESSION['username'] = $username;
    $_SESSION['password'] = $password;
    $_SESSION['name'] = $row['username'];
    session_write_close();
}
}


if(isset($_SESSION['username']))
{
   echo "<table>
<tr>
<th>username</th>";





    ?>
  <tr style="background-color:blue;color:white;"> 
          <td><font size="6"><?php echo $row['username']; ?></font></td>
          <h1 align="center"><font size="6" color="blue">welcome <?php echo $_SESSION['name']; ?></font></h1>
          <p align="right"><font size="4" color="#380B61"><a href="logout.php">Logout</a></font></td>





        </tr>
          <?php


echo "</table>";
}

else 
{
?> 
<h1 align="center"><font size="4" color="red">enter coerrect username and password</font></h1>
<td><a href="login.php">BACK</a></td>
<?php
}
?>

答案 1 :(得分:0)

这是因为您正在检查提交按钮是否被单击出站,否则您必须在刷新后重定向到登录页面

<?php

session_start();
$con=mysql_connect("localhost","root","");
if (!$con) {
    die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db('loged', $con);
if(isset($_POST["submit"])) {
$username=mysql_real_escape_string($_POST['username']); 
$password=mysql_real_escape_string($_POST['password']); 
$password=md5($password); 
 $sql="SELECT username FROM  list WHERE username='$username' and password='$password'";
$result=mysql_query($sql);
$count=mysql_num_rows($result);


if($count==1)
{
session_regenerate_id();
    $_SESSION['username'] = $username;
    $_SESSION['password'] = $password;
    session_write_close();
    echo "<table>
<tr>
<th>username</th>";



     $row = mysql_fetch_array($result) 

    ?>
  <tr style="background-color:blue;color:white;"> 
          <td><font size="6"><?php echo $row['username']; ?></font></td>
          <h1 align="center"><font size="6" color="blue">welcome <?php echo $row['username']; ?></font></h1>
          <p align="right"><font size="4" color="#380B61"><a href="logout.php">Logout</a></font></td>





        </tr>
          <?php


echo "</table>";
}

else 
{
?> 
<h1 align="center"><font size="4" color="red">enter coerrect username and password</font></h1>
<td><a href="login.php">BACK</a></td>
<?php
}
}
else {
header("location:login.php");
}
?>