php和会话重定向错误

时间:2013-02-26 10:10:26

标签: php mysql session dreamweaver session-cookies

我正在创建登录表单并使用会话注销但是当我尝试登录浏览器时显示错误(我认为错误在记录的函数中)  任何人都可以帮助我,因为它让我疯了...... **页面未正确重定向    Firefox认为服务器正在以永远不会检测到完成的方式重定向该地址的请求。

此问题有时可能是由禁用或拒绝接受引起的     饼干。**

的login.php

<?php
ob_start();
require_once('functions.php');


if(loggedin()){
    header("Location: userarea.php");
    exit();
}


if(isset($_POST['login'])){

  //get data
  $username = $_POST['username'];
  $password = $_POST['password'];
  $rememberme = $_POST['rememberme'];

  if($username && $password){

   $login = mysql_query("SELECT * FROM rememberme  WHERE username = '$username'");
   while($row = mysql_fetch_assoc($login)){

      $db_password = $row['password'];
      if($password == $db_password)
       $loginok = TRUE;
      else
        $loginok = FALSE;
      if($loginok == TRUE){

         if($rememberme == "on"){
          setcookie("username", $username, time()+7200);



         }else if($rememberme == "")
           $_SESSION['username'] = $username;
         header("Location: userarea.php");
         exit();   


      }else{
       die("incorrect username/password combination");

      }

   }



  }else
    die("please enter a username and password");
    exit();


}


?>

<form action="login.php" method="post">
<p>Username<br />
  <input type="text" name="username" />
</p>
<p> Password<br />
  <input type="password" name="password" />
</p>
<p>
  <input type="checkbox" name="rememberme"> 
  Remember me<br />

  <input type="submit" name="login" value="Log in" />
</p>
</form>
<?php ob_flush(); ?>

function.php

 <?php
 //session
 session_start();
 // connect to database 
 mysql_connect("localhost", "root", "") or die("could not connect to database");
 mysql_select_db("rememberme") or die("could not select database");

// login check function

 function loggedin()
{
    $loggedin = false;
    if(isset($_SESSION['username'])||isset($_COOKIE['username']))
    {
        $loggedin = TRUE;
    }
    return $loggedin;
}

?>

userarea.php

<?php
 //session
 session_start();
 // connect to database 
 mysql_connect("localhost", "root", "") or die("could not connect to database");
 mysql_select_db("rememberme") or die("could not select database");

// login check function

function loggedin()
{

  if(isset($_SESSION['username'])||isset($_COOKIE['username'])){
    $loggedin = TRUE;
    return $loggedin;  


  }



}

?>

1 个答案:

答案 0 :(得分:0)

您在两个文件中具有相同的功能。

function loggedin()

尝试

function loggedin()
{
    $loggedin = false;
    if(isset($_SESSION['username'])||isset($_COOKIE['username']))
    {
        $loggedin = TRUE;
    }
    return $loggedin;
}

如果if条件不满意,您的功能将不会返回真或假,因此您需要将initial值设置为false,然后if condition,则覆盖。

<强>的login.php

<?php
require_once('functions.php');

if(isset($_POST['login']))
{
    $username = $_POST['username'];
    $password = $_POST['password'];
    $rememberme = $_POST['rememberme'];

    if($username && $password)
    {
        $login = mysql_query("SELECT * FROM rememberme WHERE username = '".$username."'");
        while($row = mysql_fetch_assoc($login))
        {
            $db_password = $row['password'];
            if($password == $db_password)
                $loginok = TRUE;
            else
                $loginok = FALSE;
            if($loginok == TRUE)
            {
                if($rememberme == "on")
                {
                    setcookie("username", $username, time()+7200);
                }
                else if($rememberme == "")
                {
                    $_SESSION['username'] = $username;
                    header("Location: userarea.php");
                    exit();   
                }
            }
            else
            {
                die("incorrect username/password combination");
            }

        }
    }
    else
    {
        die("please enter a username and password");
        exit();
    }
}
?>

<form action="login.php" method="post">
    <p>Username<br />
        <input type="text" name="username" />
    </p>
    <p>Password<br />
        <input type="password" name="password" />
    </p>
    <p>
        <input type="checkbox" name="rememberme"> 
        Remember me<br />
        <input type="submit" name="login" value="Log in" />
    </p>
</form>
<?php ob_flush(); ?>

<强>的functions.php

<?php
ob_start();
session_start();
error_reporting(E_ALL);
ini_set('display_errors','On');

mysql_connect("localhost", "root", "") or die("could not connect to database");
mysql_select_db("rememberme") or die("could not select database");

// login check function

if(loggedin())
{
    header("Location: userarea.php");
    exit();
}

function loggedin()
{
    $loggedin = false;
    if(isset($_SESSION['username'])||isset($_COOKIE['username']))
    {
        $loggedin = TRUE;
    }
    return $loggedin;
}

?>

<强> userarea.php

<?php
    require_once "functions.php";
    echo "after login";
?>