重定向丢失POST / SESSION数据

时间:2016-07-08 08:57:30

标签: javascript php html mysql

我有重定向问题,我的整个代码正常工作我唯一的问题就是在这个过程中丢失了一个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 个答案:

答案 0 :(得分:1)

理想情况下,您的代码应该看起来像这样,就我看来它应该可以正常工作。我重构了你的代码并编辑了重定向语句。

panic

希望它有所帮助。如果没有随意讨论。

相关问题