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
    May 2009
    Posts
    36
    Thanks
    5
    Thanked 0 Times in 0 Posts

    passing stuff through query strings

    i send users to pages based on what they did

    Code:
    header("Location: page2.php?error=Incorrect password.");
    in page2.php i use $_GET[error] to display the error to the user.I have recently learned that this is a bad idea. In page2.php can i do something to minimize the risk?

  • #2
    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
    Being just a message, I don't see a problem with passing via get. The above won't work properly though, you'll likely want to urlencode the error message before sending it.

    Otherwise, you can use a session.
    PHP Code:
    // script calls session_start() usually at the top
    // Something went wrong:
    $_SESSION['error'] = 'Incorrect Password';
    header("Location: page2.php");
    exit(); 
    PHP Code:
    // Also needs a session start at the top
    if (isset($_SESSION['error']))
    {
        print 
    $_SESSION['error'];

    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    I usually use sessions in order to send errors to a user using a template to output the error at the top of a page in bold colours no matter what page they are visiting (This is of course assuming you are using an MVC architecture)
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #4
    New Coder
    Join Date
    May 2009
    Posts
    36
    Thanks
    5
    Thanked 0 Times in 0 Posts
    thanks. The problem is however, if i put errormessage as a session variable it gets shown in all pages!. any idea how to solve it?

  • #5
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    unset the error after you've displayed it
    PHP Code:
    if(isset($_SESSION['error'])) {
        echo 
    $_SESSION['error'];
        unset(
    $_SESSION['error']);

    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)


  •  

    Posting Permissions

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