Web Analytics Made Easy -
StatCounter Setting a Session to Javascript Value - CodingForum

Announcement

Collapse
No announcement yet.

Setting a Session to Javascript Value

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new 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'];
    ?>
    David A. Prichard

    Computer Support Tampa - Computer Support Chicago - Technology Blog

  • #2
    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!||||

    Comment


    • #3
      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...
      David A. Prichard

      Computer Support Tampa - Computer Support Chicago - Technology Blog

      Comment


      • #4
        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_; Apr 9, 2009, 09:22 AM.
        ||||If you are getting paid to do a job, don't ask for help on it!||||

        Comment


        • #5
          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
          David A. Prichard

          Computer Support Tampa - Computer Support Chicago - Technology Blog

          Comment


          • #6
            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!||||

            Comment


            • #7
              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.
              David A. Prichard

              Computer Support Tampa - Computer Support Chicago - Technology Blog

              Comment

              Working...
              X