header("Location: $url"); - not working

Discussion in 'PHP' started by sakhsen, May 23, 2012.

  1. #1
    Hi All,

    I'm quiet new to php. We are trying to submit a form for edit operation. When the form is posted, i'm trying to concatenate the existing query string. But the redirection is not working. Here is the below code. Pls help

    if ($_POST['doSubmit'] == 'Submit')
    {
    // confirm that the 'id' value is a valid integer before getting the form data
    if (is_numeric($_POST['id']))
    {
    // get form data, making sure it is valid
    $id = $_POST['id'];
    $user_email = mysql_real_escape_string(htmlspecialchars($_POST['user_email']));
    $user_name = mysql_real_escape_string(htmlspecialchars($_POST['user_name']));
    $user_ban = mysql_real_escape_string(htmlspecialchars($_POST['user_ban']));



    /* Now update user data*/
    mysql_query("update email_users set email='$user_email', name='$user_name', banned = '$user_ban' where id='$id'");


    $delqry = "delete from list_emailuser where emailid = '$id'";
    mysql_query($delqry);

    if(!empty($_POST['list'])) {
    foreach ($_POST['list'] as $lsid) {
    $lsid = filter($lsid);
    mysql_query("insert into list_emailuser(emailid,listid) values('$id','$lsid')");
    }
    }


    $ret = $_SERVER['PHP_SELF'] .'?' . $_POST['query_str'];
    header("Location: $ret");
    exit;

    }
    else
    {
    $err[]= "Unable to Update.";
    }

    }
     
    sakhsen, May 23, 2012 IP
  2. TeemuGr

    TeemuGr Greenhorn

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #2
    Are you using echo before that?
     
    TeemuGr, May 23, 2012 IP
  3. Alex Roxon

    Alex Roxon Active Member

    Messages:
    424
    Likes Received:
    11
    Best Answers:
    7
    Trophy Points:
    80
    #3
    Can't tell if serious. The line you have in that snippet is:

    [COLOR=#111111]("Location: $ret");[/COLOR]
    Code (markup):


    Not:
    header[/COLOR] [COLOR=#111111]("Location: $ret");[/COLOR]
    Code (markup):


    Also, in the future please format your code with the [ code ] tags.

    edit: forgive me. DP/vBulletin seems to behave weird when you insert header in to the page/code snippet. How odd. Anywho, make sure that block of code is being executed. Like TeemuGr stated, before using the header function, try something like:
    echo 'about to redirect'; exit;
    Code (markup):
    And see if it gets executed. If it doesn't, there's your problem. If it does, we can discuss where to proceed from there.
     
    Alex Roxon, May 23, 2012 IP
  4. sakhsen

    sakhsen Peon

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I checked with echo statement before header. It works fine. Unable to catch the bug :( but the syntax looks fine.
     
    sakhsen, May 23, 2012 IP
  5. Alex Roxon

    Alex Roxon Active Member

    Messages:
    424
    Likes Received:
    11
    Best Answers:
    7
    Trophy Points:
    80
    #5
    Output the value of $ret and let me know what it is. Can you also send me the URL so I can take a look at the HTTP headers being sent/received?
     
    Alex Roxon, May 23, 2012 IP
  6. minhazikram

    minhazikram Greenhorn

    Messages:
    46
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #6
    this is a common header() problem.
    Try deleting all the line spaces between codes. Hope it will work. Thankyou
     
    minhazikram, May 23, 2012 IP
  7. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #7
    you don't have the function name
    header("location: $ret");
    PHP:
    and then I would put a
    exit;
    PHP:
    straight after it
     
    Last edited: May 24, 2012
    stephan2307, May 24, 2012 IP
  8. e-abi

    e-abi Member

    Messages:
    122
    Likes Received:
    1
    Best Answers:
    3
    Trophy Points:
    38
    #8
    UTF-8 BOM also causes header function not to work.
    If you open PHP file in a Notepad and save it, it will almost certainly have a UTF-8 BOM.

    Use some other editor, which does not leave UTF-8 BOM-s. Notepad++ or Editplus for example.

    And turn on error reporting by placing at the start of file next clauses:

    
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    
    PHP:
    This way if header fails, then error is printed to output.
     
    e-abi, May 24, 2012 IP
  9. e-abi

    e-abi Member

    Messages:
    122
    Likes Received:
    1
    Best Answers:
    3
    Trophy Points:
    38
    #9
    One more thing:
    Location header should contain absolute URL path. $_SERVER['PHP_SELF'] is relative and that is why it does not work.
    Yes on some browsers relative location works, but specifications require absolute url.
     
    e-abi, May 24, 2012 IP
  10. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #10
    I'm not a fan of using "exit;". I think programs should be written in a way that the script exits naturally. Calling a termination command can create problems down the road.
     
    NetStar, May 24, 2012 IP
  11. sakhsen

    sakhsen Peon

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Hi All,

    Thanks to all for the reply messages. I removed the redirection code - header("Location: $ret") and displayed success and error message. That way :) the client has agreed. Some way we have solved though unable to sort out the bug.

    Thanks again all of you!
     
    sakhsen, May 24, 2012 IP