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 7 of 7
  1. #1
    Regular Coder
    Join Date
    Jul 2004
    Location
    Tampa
    Posts
    223
    Thanks
    23
    Thanked 0 Times in 0 Posts

    Setting a Session to Javascript Value

    I am having an issue with setting a session called referrer to a value. What I am trying to do is if someone comes to the site, set the page they came from in a session so I can use it if they submit a form later to tell where they came from originally. I only want to do this the first time they come to a page and don't want it to change as they browse the site. The problem is that when I try it like this it is saving the javascript as the session so it changes on every page cause the session is equal to the javascript and not the value written out by the javascript. Any help would be greatly appreciated.

    PHP Code:
    <?php 
        
    if(isset($_SESSION['referrer']))
            {}
        else 
            {    
                
    $_SESSION['referrer'] = '<script>document.write(document.referrer);</script>';
            }
        echo 
    $_SESSION['referrer'];
    ?>

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    You should know that the referrer isn't very reliable but I think you are going about this the wrong way. Just use php's built in stuff. e.g.
    PHP Code:
    <?php 
    session_start
    (); // not sure if you have this line anywhere yet, I'm assuming you do but put it in here just in case
        
    if(!isset($_SESSION['referrer'])) 
            {    
                
    $_SESSION['referrer'] = $_SERVER['HTTP_REFERER'];
            }
        echo 
    $_SESSION['referrer'];
    ?>
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #3
    Regular Coder
    Join Date
    Jul 2004
    Location
    Tampa
    Posts
    223
    Thanks
    23
    Thanked 0 Times in 0 Posts
    Yeah, I have been reading that it isn't very reliable. I looked at my PHP info and I don't have HTTP_REFERRER in there so it isn't working for me. Do you know how I add it?

    Thanks for the prompt response and DANG, you have a lot of posts...

  • #4
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    You don't add it. Its a server global. It will only work if A) your browser is sending it and B) You navigated to that page from a different page meaning there has to be history in the user's browser. Just because its not in your phpinfo file doesn't mean you don't have it. I looked at my phpinfo, its not there but when I tested it, it worked fine. The better thing to do would be
    PHP Code:
    <?php 
    session_start
    (); // not sure if you have this line anywhere yet, I'm assuming you do but put it in here just in case
        
    if(!isset($_SESSION['referrer']) && isset($_SERVER['HTTP_REFERER'])) 
            {    
                
    $_SESSION['referrer'] = $_SERVER['HTTP_REFERER'];
        echo 
    $_SESSION['referrer'];
            }

    ?>
    Now your session will only be set if there is something in the REFERER. Also phpinfo will only show the referer if its not empty. In your case it likely was empty.
    Last edited by _Aerospace_Eng_; 04-09-2009 at 02:22 PM.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • Users who have thanked _Aerospace_Eng_ for this post:

    dprichard (04-09-2009)

  • #5
    Regular Coder
    Join Date
    Jul 2004
    Location
    Tampa
    Posts
    223
    Thanks
    23
    Thanked 0 Times in 0 Posts
    This is what I get if I try to use it though:

    PHP Code:
    <?php 
    echo $_SERVER['HTTP_REFERER'];
    ?>
    PHP Notice: Undefined index: HTTP_REFERER in C:\inetpub\wwwroot\oursite.com\test.php on line 2

  • #6
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Read my previous post. Its doing that because the referer is empty so its not set yet. The referer is only set when there is a value in it.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #7
    Regular Coder
    Join Date
    Jul 2004
    Location
    Tampa
    Posts
    223
    Thanks
    23
    Thanked 0 Times in 0 Posts
    AAAAHHHHHH, Ok... It is because I am going right to my test page and it doesn't have a referrer. I get it now. Kewl, thank you so much for your help.


  •  

    Posting Permissions

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