2016-07-08 41 views
-1

我有一個重定向的問題,我的整個代碼工作我唯一的問題是丟失過程中的POST/SESSION數據。花了無數個小時工作,並嘗試了很多工作,但仍然無效,這是我唯一的問題。這裏是我的代碼重定向丟失開機自檢/會話數據

<?php 
// First we execute our common code to connection to the database and start the session 
require("common.php"); 

// This variable will be used to re-display the user's username to them in the 
// login form if they fail to enter the correct password. It is initialized here 
// to an empty value, which will be shown if the user has not submitted the form. 

// This if statement checks to determine whether the login form has been submitted 
// If it has, then the login code is run, otherwise the form is displayed 
if(!empty($_POST)) { 
    // This query retreives the user's information from the database using 
    // their username. 
    if(isset($_POST['validEmail'])) { 
      $query = "SELECT * 
        FROM registered_email 
        WHERE email = :validEmail"; 
    } 

    // The parameter values 
    $query_params = array(':validEmail' => $_POST['validEmail']); 

    try { 
     // Execute the query against the database 
     $stmt = $db->prepare($query); 
     $result = $stmt->execute($query_params); 
    } 
    catch(PDOException $ex) { 
     // Note: On a production website, you should not output $ex->getMessage(). 
     // It may provide an attacker with helpful information about your code. 
     die("Failed to run query"); 
    } 

    // This variable tells us whether the user has successfully logged in or not. 
    // We initialize it to false, assuming they have not. 
    // If we determine that they have entered the right details, then we switch it to true. 
    $login_ok = false; 

    // Retrieve the user data from the database. If $row is false, then the username 
    // they entered is not registered. 
    $row = $stmt->fetch(); 
    if($row) { 

     if($_POST['validEmail'] === $row['email']) { 
      // If they do, then we flip this to true 
      $login_ok = true; 
     } 
    } 

    // If the user logged in successfully, then we send them to the private members-only page 
    // Otherwise, we display a login failed message and show the login form again 
    if($login_ok) { 
     $_SESSION['sesEmail'] = $row; 

     // Redirect the user to the private members-only page. 
     if (isset($_POST['validEmail'])) { 
      echo "<script>location='http://www.url.com.ph/some.php'</script>"; 
     } 
    } else { 
     // Tell the user they failed 
     print "Sorry to say that your Email is not Registered!."; 
    } 
} 
+1

你確定要在所有的頭文件中設置session_start()嗎? – rad11

+0

我用ob_start();現在,嘗試session_start();太。仍然沒有運氣。嘗試自己發佈它,POST的作品。但是當重定向它消失了。 – jmv

+0

你不能用session_start()來嘗試你必須擁有它,顯示通用文件 – rad11

回答

1

理想情況下,你的代碼應該看起來像這樣,它應該工作得很好,據我所知。我重構了你的代碼並編輯了重定向語句。

// I am assuming you have session_start(); included in common.php 
require("common.php"); 

if(!empty($_POST)) { 
    if(isset($_POST['validEmail'])) { 
     $query = "SELECT * 
       FROM registered_email 
       WHERE email = :validEmail"; 
     $query_params = array(':validEmail' => $_POST['validEmail']); 
     try { 
      $stmt = $db->prepare($query); 
      $result = $stmt->execute($query_params); 
     } 
     catch(PDOException $ex) { 
      die("Failed to run query"); 
     } 
     $login_ok = false; 
     $row = $stmt->fetch(); 
     if($row) { 
      if($_POST['validEmail'] === $row['email']) { 
       $login_ok = true; 
      } 
     } 
     if($login_ok) { 
      $_SESSION['sesEmail'] = $row; 
      if (isset($_POST['validEmail'])) { 
       // the page where you are redirecting should be linked with session as well 
       echo "<script>window.location.href='http://www.url.com.ph/some.php'</script>"; 
      } 
     } else { 
      // Tell the user they failed 
      print "Sorry to say that your Email is not Registered!."; 
     } 
    } 
    else { 
     // Tell the user they failed 
     print "Sorry no POST parameters!."; 
    } 
} 

希望它有幫助。如果不願意討論。