Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Sep 2012
    Posts
    76
    Thanks
    61
    Thanked 0 Times in 0 Posts

    Access $POST data using header re-direct?

    Hello, I am looking for some help with a form I am designing please.

    I have a form, where a user enters their $_POST['user_id'] and their $_POST['name'].

    Now whatever number they enter for their $_POST['user_id'] and hit submit on the form I use to make a URL string for examlple

    search/123456/
    Or
    search/123473/

    Which is done using a header redirect

    PHP Code:
    header('Location: search/'.$_POST['user_id']).'');
    exit; 
    However, on the page I am re-directing to I also want to be able to access
    echo the $_POST['name'].

    Is it possible without passing the name in the URL string, or is there a way around this?

    Thank you

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,313
    Thanks
    23
    Thanked 612 Times in 611 Posts
    Not gonna work. Why don't you simple do the search on the script the form submits to?

  • #3
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,343
    Thanks
    60
    Thanked 527 Times in 514 Posts
    Blog Entries
    4
    Quote Originally Posted by Oatley View Post
    However, on the page I am re-directing to I also want to be able to access
    echo the $_POST['name'].

    Is it possible without passing the name in the URL string, or is there a way
    Contrary to sunfighter, yes. Sessions.

    A session is basically a bit of memory you can read and write which php automatically saves and reads from a file on the servers hard disk. It is unique to each user and identified by a cookie.

    At the top of your script BEFORE any output such as html or print statements AND in every script that use the $_SESSION array:
    session_start();

    Then after:
    $_SESSION['name'] = $_POST['name'];

    Likewise to access it in your next script:
    session_start();
    $Name = $_SESSION['name'];

    I'm not sure why sunfighter thinks this isn't possible..
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • Users who have thanked tangoforce for this post:

    Oatley (12-05-2012)

  • #4
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,313
    Thanks
    23
    Thanked 612 Times in 611 Posts
    I didn't think it would work because header('Location: search/'.$_POST['user_id']).''); just didn't make sense to me.

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    These are both correct.
    $_POST becomes unusable. You cannot post and get. You should be dealing with post at the location where you intend to post.
    $_SESSION can be populated with $_POST. So you can retrieve $_SESSION['post'] for example from the page redirected to.

    The final solution doesn't deal with redirection. You can connect to the page via socketing or curl and send the POST data manually.

  • Users who have thanked Fou-Lu for this post:

    Oatley (12-06-2012)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •