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 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jun 2010
    Location
    Los Angeles, CA
    Posts
    7
    Thanks
    3
    Thanked 1 Time in 1 Post

    Question Header Location Call w/URL in Session Variable?

    I am a new php programmer, and have a dumb question that is causing me to bang my head this morning.

    I have a URL stored in a SESSION variable named "refer". I need to jump to that URL, but keep get syntax errors. Can anyone tell me the correct syntax to do this ... ???

    PHP Code:
    header'Location: $_SESSION['refer']' ) ; 
    does not work, yields the error "Parse error: syntax error, unexpected T_STRING in C:\xampp\htdocs\choose-mod_beta1.php on line 26".

    I've tried several variations of the above call, but I think I'm missing something obvious.

    What is the correct syntax to use the location call with a URL stored in a session variable?

    Thanks in advance!

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    You are embedding a variable inside a string declaration. There are a few rules you have to follow.

    1. You must use double-quotes when embedding variables
    2. If the variable is "complex", that is to say, an array or object reference, the variable must ben enclosed in squiggly brackets.

    Following these two rules your code would look like this:

    PHP Code:
    header"Location: {$_SESSION['refer']}" ) ; 
    The full list of rules can be found in the PHP manual. Look for the section on variable parsing.

    http://us3.php.net/manual/en/language.types.string.php

    (p.s. don't fall into the sloppy trap of omitting the quotes around your array index name (i.e. "Location: $_SESSION[refer]'"). This passes the PHP syntax check but is the wrong way to go, as PHP has to convert the index name to a constant to get it to work.)
    Last edited by Fumigator; 07-06-2010 at 07:02 PM.

  • Users who have thanked Fumigator for this post:

    gregs (07-06-2010)

  • #3
    New to the CF scene
    Join Date
    Jun 2010
    Location
    Los Angeles, CA
    Posts
    7
    Thanks
    3
    Thanked 1 Time in 1 Post
    That works perfectly - thank you, Sir! And I especially appreciate the reference to the online manual. I was using the W3schools reference, and missed what you are calling out there.

  • #4
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,536
    Thanks
    45
    Thanked 259 Times in 256 Posts
    You can also do a concatenation.

    PHP Code:
    header('Location: '.$_SESSION['refer']); 

  • #5
    Regular Coder byuhobbes85's Avatar
    Join Date
    Oct 2006
    Location
    Ames, Iowa, USA
    Posts
    116
    Thanks
    9
    Thanked 4 Times in 4 Posts
    I've seen it done in all of these ways. The first is probably the best as it is the most explicit, while the last is worst in terms of maintenance.
    PHP Code:
    header("Location: "$_SESSION['refer']); 
    PHP Code:
    header("Location: {$_SESSION['refer']}"); 
    PHP Code:
    header("Location: $_SESSION[refer]"); 
    -- </byuhobbes>

  • #6
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    The last one is plain wrong. Find the section "Array do's and dont's" on this page:

    http://us3.php.net/manual/en/language.types.array.php


  •  

    Tags for this Thread

    Posting Permissions

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