How to store referring URL in a Session variable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rahia307
    New Member
    • Jun 2007
    • 32

    How to store referring URL in a Session variable

    hi
    every body.
    i have problem in login.
    the problem is that
    i want to redirect page where i come from login
    in other word i want to store url in session
    please help me for it
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    If you follow a link into a .php page, PHP stores the URL of the page you came from in the &_SERVER['HTTP_REFERER'] index.

    So when you enter your login form you could do something like this to get a link back to the page you came from:
    [code=php]
    echo '<a href="'. $_SERVER['HTTP_REFERER'] . '">Go back</a>';
    [/code]

    You can also get the current page you are on by using the $_SERVER['PHP_SELF'] index.
    This on is defferent, however, as it does not store the host URL.
    That is, if the URL is: http://mypage.com/files/index.php
    the PHP_SELF stores: /files/index.php

    Comment

    • pbmods
      Recognized Expert Expert
      • Apr 2007
      • 5821

      #3
      Changed thread title to better describe the problem.

      Comment

      • rahia307
        New Member
        • Jun 2007
        • 32

        #4
        Originally posted by Atli
        If you follow a link into a .php page, PHP stores the URL of the page you came from in the &_SERVER['HTTP_REFERER'] index.

        So when you enter your login form you could do something like this to get a link back to the page you came from:
        [code=php]
        echo '<a href="'. $_SERVER['HTTP_REFERER'] . '">Go back</a>';
        [/code]

        You can also get the current page you are on by using the $_SERVER['PHP_SELF'] index.
        This on is defferent, however, as it does not store the host URL.
        That is, if the URL is: http://mypage.com/files/index.php
        the PHP_SELF stores: /files/index.php





        hi
        thanks
        but it redirect same page login page
        but i want to redirect before login i visited page
        please help me

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          Ok, lets see...

          The REFERER index only stores the last page you were on. So you would need your code to remember the REFERER value as it was when you entered your login page.

          Lets say I have a login.php where I have a form that, when submitted, sends me to a submit.php, which processes the form data. Now, if I call REFERED in the submit page, I get sent back to the login page, which is not what we want.

          What I need to do, is store the REFERER value as it is when I enter the login.php page. To do this I can use the $_SESSION array.
          But I must be carefull not to write the loging page in the SESSION if the REFERER value is the login.php, which could happen if there is a link to it somwhere.
          So, my login.php page would start like this:
          [code=php]
          // Start a session
          session_start() ;

          // Make sure the REFERER value is
          // not pointing to the login.php page.
          if(strstr($_SES SION['HTTP_REFERER'], "login.php" ) == false)
          {
          // Save the value in the $_SESSION array
          $_SESSION['beforeLogin'] = $_SERVER['HTTP_REFERER'];
          }
          [/code]

          Now, in the submit page, after I proccess the form data, I will have to redirect to the value saved in the $_SESSION array.
          That might look something like this:
          [code=php]
          // Start session
          session_start() ;

          // Make sure the session variable is set.
          // If it is not, the login page must not
          // have had any value to save so we
          // must redirect somewhere else.
          if(!isset($_SES SION['beforeLogin']))
          {
          // Redirect to some general place
          header("Locatio n: http://mypage.com/home.php");
          }
          else
          {
          // Lets get our variable
          $redirect = $_SESSION['beforeLogin'];

          // It is always good to unset session
          // variables when we stop using them.
          unset($_SESSION['beforeLogin']);

          // And now we redirect
          header("Locatio n: ". $redirect);
          }
          [/code]

          I hope this clears it up a bit.

          Comment

          • rahia307
            New Member
            • Jun 2007
            • 32

            #6
            Originally posted by Atli
            Ok, lets see...

            The REFERER index only stores the last page you were on. So you would need your code to remember the REFERER value as it was when you entered your login page.

            Lets say I have a login.php where I have a form that, when submitted, sends me to a submit.php, which processes the form data. Now, if I call REFERED in the submit page, I get sent back to the login page, which is not what we want.

            What I need to do, is store the REFERER value as it is when I enter the login.php page. To do this I can use the $_SESSION array.
            But I must be carefull not to write the loging page in the SESSION if the REFERER value is the login.php, which could happen if there is a link to it somwhere.
            So, my login.php page would start like this:
            [code=php]
            // Start a session
            session_start() ;

            // Make sure the REFERER value is
            // not pointing to the login.php page.
            if(strstr($_SES SION['HTTP_REFERER'], "login.php" ) == false)
            {
            // Save the value in the $_SESSION array
            $_SESSION['beforeLogin'] = $_SERVER['HTTP_REFERER'];
            }
            [/code]

            Now, in the submit page, after I proccess the form data, I will have to redirect to the value saved in the $_SESSION array.
            That might look something like this:
            [code=php]
            // Start session
            session_start() ;

            // Make sure the session variable is set.
            // If it is not, the login page must not
            // have had any value to save so we
            // must redirect somewhere else.
            if(!isset($_SES SION['beforeLogin']))
            {
            // Redirect to some general place
            header("Locatio n: http://mypage.com/home.php");
            }
            else
            {
            // Lets get our variable
            $redirect = $_SESSION['beforeLogin'];

            // It is always good to unset session
            // variables when we stop using them.
            unset($_SESSION['beforeLogin']);

            // And now we redirect
            header("Locatio n: ". $redirect);
            }
            [/code]

            I hope this clears it up a bit.


            hi
            thanks for reply
            but it redirect me again in login page after login.
            one thing i tell u i use function for it
            and i have process page for it
            please help me

            Comment

            • xiaawan
              New Member
              • May 2007
              • 17

              #7
              Dear I viewed your problem yesterday. And I had a similar problem . I solved my problem using following function.

              function getCurrentPageU rl()
              {
              $pageURL = 'http';

              if ($_SERVER["HTTPS"] == "on")
              {
              $pageURL .= "s";
              }

              $pageURL .= "://";

              if ($_SERVER["SERVER_POR T"] != "80")
              {
              $pageURL .= $_SERVER["SERVER_NAM E"].":".$_SERVE R["SERVER_POR T"].$_SERVER["REQUEST_UR I"];
              }
              else
              {
              $pageURL .= $_SERVER["SERVER_NAM E"].$_SERVER["REQUEST_UR I"];
              }
              $_SESSION['CURRENT_PAGE_U RL'] = $pageURL;

              }

              Call this function to every page that can be redirected after login
              And check at login code. If there is any value in the session your page will be redirected to the value stored in the session. I have implemented this function and its working fine. I hope it will work for you as well

              if ($_SESSION['CURRENT_PAGE_U RL'])
              {
              header('locatio n: '.$_SESSION['CURRENT_PAGE_U RL']);
              }
              else
              {
              //redirect to default url
              }

              Comment

              Working...