开发者

Redirecting if statement not working

开发者 https://www.devze.com 2022-12-17 12:50 出处:网络
Hey, I am trying to make an if statement that redirects them to a different page if true, simple right?

Hey, I am trying to make an if statement that redirects them to a different page if true, simple right?

I am not sure why this is not working but I am using:

if ($_POST['accounttype']=='Paid User - £2 p/m'){
    $userid = strtolower($_SESSION['X2X2']);
    $getuser = mysql_query("SELECT * FROM XXXXXX WHER开发者_如何学PythonE X2X2 = '$userid'");
    $info = mysql_fetch_array($getuser);
    $id = $info['X3X3'];
    mysql_query("UPDATE members SET payment = '" . mysql_real_escape_string("XXXXXXXX"). "' WHERE X3X3 = $id");
    header('Location: http://beta.XXXXX.co.uk/purchase.php');
    mysql_close($con);
}

When I put

<?
echo $_POST['accounttype'];
?>

And I get back

Paid User - £2 p/m

Which is correct?

Any help would be appreciated, Thanks.


Looks like you want to call exit() before the close brace on your if statement.

The documentation for header has example code like this:

<?php
header("Location: http://www.example.com/"); /* Redirect browser */

/* Make sure that code below does not get executed when we redirect. */
exit;
?>

The end bit of your if statement really ought to be:

mysql_query("UPDATE members SET payment = '" . mysql_real_escape_string("XXXXXXXX"). "' WHERE X3X3 = $id");
mysql_close($con); // do this before sending a redirect header
header('Location: http://beta.XXXXX.co.uk/purchase.php');
exit();

Also, header doesn't work if you've already sent any output, per this warning from the documentation for header:

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include(), or require(), functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.


As it seems to depend on £, you have several possibilities depending on which values $_POST['accounttype'] can have.

First I suggest you try:

if ($_POST['accounttype']=='Paid User - &pound;2 p/m'){

(as &pound; is £ in HTML).
If this doesn't work, what is the part of the string, that makes it unique? Paid User or 2 p/m? If any of these, it is sufficient to check against a substring like:

if (substr($_POST['accounttype'],-5)=='2 p/m'){

or

if (substr($_POST['accounttype'],0,9)=='Paid User'){

or any combination (avoiding £).


You haven't by any chance already output something to the browser have you? If you modify the location header after using the echo or print statements, it will issue a warning which you probably won't see unless you have verbose errors or logging turned on.

I know this can happen with UTF-8 files in some versions of PHP - the byte order mark (BOM) of the UTF-8 file are output before the PHP script starts execution, which prevents the location header from being sent.


Altering the HTTP header with header requires that the HTTP header has not been sent yet. This can be one reason for why it doesn’t work for you as the HTTP header is sent together with the first output of your script (any output including text before <?php).

When you set error_reporting to E_ALL and display_errors to true, PHP will display you all errors immediately. This can help you to determine the cause of you error.


My first inclination would be to check if there are any extra characters on your POST data by trying the following:

if (trim($_POST['accounttype']) == 'Paid User - £2 p/m') {
0

精彩评论

暂无评论...
验证码 换一张
取 消