Why is my page not redirecting on success

时间:2015-07-28 23:18:39

标签: php

I have some code that I was given; there is a $_POST variable (email) that shows as having content, yet when I test for it using isset, it fails. I don't understand why it's failing; any help would be greatly appreciated!

This is the PHP code:

<?php 
session_start();

echo "POST variables<br>";
var_dump($_POST);

echo "<br><br>SESSION variables<br>";
var_dump($_SESSION);


$itemcount = isset($_SESSION['itemcount']) ? $_SESSION['itemcount'] : 0;
if ($itemcount == 0)  {
header("Location: "."error.php?msg=".rawurlencode("Please add items to your shopping cart before checking out."));
exit;
} 

echo "POST email value: ".($_POST['email'])."<br>";

if (!isset($_POST['email']))  {  //  <--  FAILS HERE
header("Location: "."error.php?msg=".rawurlencode("We did not find your information, please enter the needed information again."));
exit;
} 

echo "server request method: ".$_SERVER['REQUEST_METHOD'];
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
$_SESSION['shipname'] = $_POST['shipname'];
$_SESSION['shipaddress'] = $_POST['shipaddress'];
$_SESSION['shipzip'] = $_POST['shipzip'];
$_SESSION['shipcity'] = $_POST['shipcity'];
$_SESSION['shipstate'] = $_POST['shipstate'];

$_SESSION['paymenttype'] = "PayPal";
header("Location: "."thankyou.php");
} 

$shipname = isset($_SESSION['shipname']) ? $_SESSION['shipname'] : '';
$shipaddress = isset($_SESSION['shipaddress']) ? $_SESSION['shipaddress'] : '';
$shipzip = isset($_SESSION['shipzip']) ? $_SESSION['shipzip'] : '';
$shipcity = isset($_SESSION['shipcity']) ? $_SESSION['shipcity'] : '';
$shipstate = isset($_SESSION['shipstate']) ? $_SESSION['shipstate'] : '';

?>

And these are the results of the echo statements:

POST variables
array(11) { 
    ["lastname"]=> string(6) "dfgdfg" 
    ["email"]=> string(22) "kelliemmarsh@gmail.com" 
    ["city"]=> string(8) "van nuys" 
    ["fax"]=> string(0) "" 
    ["state"]=> string(2) "WA" 
    ["firstname"]=> string(5) "dfbdf" 
    ["address2"]=> string(0) "" 
    ["phone"]=> string(12) "509-555-1414" 
    ["zip"]=> string(5) "90266" 
    ["address"]=> string(14) "123 any street" 
    ["Continue"]=> string(8) "Continue" 
}
SESSION variables
array(8) { 
    ["cart"]=> array(4) { 
        [0]=> array(6) { 
            [0]=> string(6) "ch-002" 
            [1]=> string(6) "ch-002" 
            [2]=> string(0) "" 
            [3]=> string(0) "" 
            [4]=> string(0) "" 
            [5]=> string(0) "" 
        } 
        [1]=> array(6) { 
            [0]=> string(26) "Fish Wavy Stripe Turquoise" 
            [1]=> string(26) "Fish Wavy Stripe Turquoise" 
            [2]=> string(0) "" 
            [3]=> string(0) "" 
            [4]=> string(0) "" 
            [5]=> string(0) "" 
        } 
        [2]=> array(6) { 
            [0]=> int(1) 
            [1]=> int(2) 
            [2]=> string(0) "" 
            [3]=> string(0) "" 
            [4]=> string(0) "" 
            [5]=> string(0) "" 
        } 
        [3]=> array(6) { 
            [0]=> string(5) "10.99" 
            [1]=> string(5) "10.99" 
            [2]=> string(0) "" 
            [3]=> string(0) "" 
            [4]=> string(0) "" 
            [5]=> string(0) "" 
        } 
    } 
    ["itemcount"]=> int(2) 
    ["shipname"]=> NULL 
    ["shipaddress"]=> NULL 
    ["shipzip"]=> NULL 
    ["shipcity"]=> NULL 
    ["shipstate"]=> NULL 
    ["paymenttype"]=> string(6) "PayPal" 
}

POST email value: kelliemmarsh@gmail.com
server request method: POST 

**UPDATE: This is the revised code with all of the 'echo' statements removed:

<?php 
session_start();

$itemcount = isset($_SESSION['itemcount']) ? $_SESSION['itemcount'] : 0;
if ($itemcount == 0)  {
header("Location: error.php?msg=rawurlencode("Please add items to your shopping cart before checking out."));
exit;
} 

if (empty($_POST['email']))  {
header("Location: "."error.php?msg=".rawurlencode("We did not all of the required information."));
exit;
} 

else {
$_SESSION['shipname'] = $_POST['shipname'];
$_SESSION['shipaddress'] = $_POST['shipaddress'];
$_SESSION['shipzip'] = $_POST['shipzip'];
$_SESSION['shipcity'] = $_POST['shipcity'];
$_SESSION['shipstate'] = $_POST['shipstate'];

$_SESSION['paymenttype'] = "PayPal";


$shipname = isset($_SESSION['shipname']) ? $_SESSION['shipname'] : '';
$shipaddress = isset($_SESSION['shipaddress']) ?    $_SESSION['shipaddress'] : '';
$shipzip = isset($_SESSION['shipzip']) ? $_SESSION['shipzip'] : '';
$shipcity = isset($_SESSION['shipcity']) ? $_SESSION['shipcity'] : '';
$shipstate = isset($_SESSION['shipstate']) ? $_SESSION['shipstate'] : '';

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

3 个答案:

答案 0 :(得分:3)

The reason why the redirect with String [] baseValues = {"Awesome", "Perfect", "Happy", "Good"}; Scanner scan = new Scanner(); String userInput = scan.nextLine(); String [] inputArray = userInput.split(" "); is not working is, that you already sent a header by echoing something.

Any header-redirection won´t work afterwards.

答案 1 :(得分:0)

Try :

if (empty($_POST['email']))  {  
header("Location: "."error.php?msg=".rawurlencode("We did not find your information, please enter the needed information again."));
exit;
} 

答案 2 :(得分:-1)

Take out the !. That should fix it: if you enter a valid email you are negating true (which becomes false).

Keep in mind that when you use isset() it returns a boolean (true or false)