我必须登录两次,才能登录?

时间:2011-04-01 08:28:17

标签: php

我的网站有一个登录脚本,由于某种原因你必须登录,然后它会回到登录页面然后再次登录并且它很好,它必须与登录脚本有关,但不确定其中...

    <?php
    $host="localhost"; // Host name
    $username=""; // Mysql username
    $password=""; // Mysql password
    $db_name=""; // Database name
    $tbl_name="members"; // Table name

   // Connect to server and select databse.
   mysql_connect("$host", "$username", "$password")or die("cannot connect");
   mysql_select_db("$db_name")or die("cannot select DB");

   // username and password sent from form
   $myusername=$_POST['myusername'];
   $mypassword=$_POST['mypassword'];

   // To protect MySQL injection (more detail about MySQL injection)
   $myusername = stripslashes($myusername);
   $mypassword = stripslashes($mypassword);
   $myusername = mysql_real_escape_string($myusername);
   $mypassword = mysql_real_escape_string($mypassword);

   $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and    password='$mypassword'";
   $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){
        session_start();
       session_regenerate_id();
        $_SESSION['SESS_USERNAME'] = $myusername;
        $_SESSION['SESS_PASSWORD'] = $mypassword;
        session_write_close();
       header("location: login_success.php");
   }

   else 
  {
  include("loginhtml.html");
  } 
 ?>

然后是checklogin,(位于每页的顶部)

          <?php
          session_start();
          if(!isset($_SESSION['SESS_USERNAME']) || !isset($_SESSION['SESS_PASSWORD']) || (trim($_SESSION['SESS_USERNAME']) == '')) {
            //someone's not logged in
    header("location: index.php"); //it's suppose to actually be there
            exit();
          }
         ?>

任何想法??

欢呼声

2 个答案:

答案 0 :(得分:0)

你需要像这样做

$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name
$tbl_name="members"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");



session_start();


if(isset($_POST))
{
    // username and password sent from form
    $myusername=$_POST['myusername'];
    $mypassword=$_POST['mypassword'];

    // To protect MySQL injection (more detail about MySQL injection)
    $myusername = stripslashes($myusername);
    $mypassword = stripslashes($mypassword);
    $myusername = mysql_real_escape_string($myusername);
    $mypassword = mysql_real_escape_string($mypassword);

    $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and    password='$mypassword'";
    $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){
        session_regenerate_id();
        $_SESSION['SESS_USERNAME'] = $myusername;
        $_SESSION['SESS_PASSWORD'] = $mypassword;
        session_write_close();
        header("location: login_success.php");
    }
    else
    {
        include("loginhtml.html");
    } 
}

答案 1 :(得分:0)

按照此图

index.php
 -db connect

 -if $_POST and ok
   --redirect to loginsuccess
  else
   --show loginerror

 -if no session
   show login form