2013-03-04 94 views
0

我正在創建一個使用會話的簡單登錄形式,但問題是當我按下登錄名時,它將我重定向到index.php但我需要去主頁。 php。在logout.php我破壞了會議,我重定向到的index.php,但好歹是登錄按鈕重定向我到的index.php像療法沒有在登錄過程中的成功如何解決這個錯誤我非常需要。會話+ php + mysql +錯誤

的index.php
<?php 
require_once('global.php'); 

if(@$logged == 1) 
{ 

    header("Location: home.php"); 
    exit(); 
} 

?> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>index page</title> 
</head> 

<body> 
<h1> this is the index page</h1> 
<a href="login.php">Login</a> 
</body> 
</html> 

global.php
<?php 
session_start(); 
require_once('connect.php'); 

// cheking if the sessions are set 

if(isset($_SESSION['username'])) 
{ 
    $session_username = $_SESSION['username']; 
    $session_pass = $_SESSION['password']; 
    $session_id = $_SESSION['id']; 

    //cheking if the member exist 

    $query = mysql_query("SELECT * FROM members WHERE id = '".$session_id."' AND password = '".$session_pass."' LIMIT 1") or die("could not select memeber"); 
    $count_count = mysql_num_rows($query); 
    if($count_count > 0) 
    { 
     $logged = 1; 
     while($row = mysql_fetch_array($query)) 
     { 
      $session_username = $row['username']; 
     } 
     $_SESSION['username'] = $session_username; 
     $_SESSION['pass'] = $session_pass; 
     $_SESSION['id'] = $session_id;   

    } 
    else 
    { 
     header("Location: logout.php"); 
     exit(); 
    } 
} 
else 
{ 
    // if the user not loged in 
    $logged = 0; 

} 

?> 

的login.php
<?php 
require_once('global.php'); 


$message = ""; 
if(isset($_POST['email'])) 
{ 
    $email = $_POST['email']; 
    $pass = $_POST['password']; 

    // error handling 
    if((!$email) ||(!$pass)) 
    { 
     $message = 'please insert both fields'; 

    } 
    else 
    { 
     //secure data 
     $email = mysql_real_escape_string($email); 
     $pass = sha1($pass); 
     $query = mysql_query("SELECT * FROM members WHERE email = '".$email."' AND password = '".$pass."'LIMIT 1") or die("could not select data"); 
     $count_query = mysql_num_rows($query); 
     if($count_query == 0) 
     { 
      $message = 'your info was inccorrect'; 
     } 
     else 
     { 
      //start SESSIONS 
      $_SESSION['pass'] = $pass; 
      while($row = mysql_fetch_array($query)) 
      { 

       $username = $row['username']; 
       $id = $row['id']; 
      } 
      $_SESSION['username'] = $username; 
      $_SESSION['id'] = $id; 

     } 
     header("Location: home.php"); 
    } 





} 


?> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>login to membership website </title> 
</head> 

<body> 
<h1> login to my website</h1> 
<p><?php print("$message"); ?></p> 
<form action="login.php" method="post"> 
<input type="text" name="email" placeholder="email adress" /><br /> 
<input type="password" name="password" placeholder="password" /><br /> 

<input type="submit" value="Login" /> 
</form> 


</body> 
</html> 

home.php
<?php 
require_once('global.php'); 
if($logged == 0) 
{ 

    header("Location: index.php"); 
    exit(); 
} 
?> 


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 
</head> 

<body> 
<h1>this the home page</h1> 
</body> 
</html> 

logout.php

<?php 
session_start(); 

session_destroy(); 
/* 
if(session_is_registered('username')) 
{ 

    echo "you are loged in we can not log you out"; 
    exit(); 

} 
*/ 
//else 
//{ 

    header("Location: index.php"); 
//} 


?> 
+0

不要使用@它來抑制錯誤 – ScoRpion 2013-03-04 08:04:24

+0

@ ScoRpion如果我做n ot把@它顯示一個錯誤,說UNdifine索引登錄行..... – 2013-03-04 08:07:46

回答

0

當您檢查與$ _SESSION會議[ '用戶名'],你不需要登錄變量。 當$ _SESSION ['用戶名']存在時,您可以允許用戶訪問該頁面,並且如果它沒有將他重定向到登錄頁面

+0

所以你建議我在哪裏做我的代碼更改? – 2013-03-04 08:41:38

0

說實話,這是相當麻煩的編碼,有點亂,但問題是,這樣的login.php重定向到home.php然後home.php重定向到index.php

那麼試試這個

的login.php

login.php中沒有設置$登錄= TRUE
<?php 
require_once('global.php'); 

$message = ""; 
if(isset($_POST['email'])) { 
    $email = $_POST['email']; 
    $pass = $_POST['password']; 

    // error handling 
    if((!$email) ||(!$pass)) { 
     $message = 'please insert both fields'; 
    } 
    else 
    { 
     //secure data 
     $email = mysql_real_escape_string($email); 
     $pass = sha1($pass); 
     $query = mysql_query("SELECT * FROM members WHERE email = '".$email."' AND password = '".$pass."'LIMIT 1") or die("could not select data"); 
     $count_query = mysql_num_rows($query); 
     if($count_query == 0) { 
      $message = 'your info was inccorrect'; 
     } else { 
      //start SESSIONS 
      $_SESSION['pass'] = $pass; 
      while($row = mysql_fetch_array($query)) { 
       $username = $row['username']; 
       $id = $row['id']; 
      } 
      $_SESSION['username'] = $username; 
      $_SESSION['id'] = $id; 
      // NEW LINE 
      $logged = 1; 
     } 
     header("Location: home.php"); 
    } 
} 
?>