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 11 of 11
  1. #1
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    php session, hold current url

    Hey guys, I was wondering if there was an easy way, in a session variable, to store what url the user is currently viewing? Thanks for any help.

  • #2
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    or a way to remember last where the user was, so i can redirect them back there.

  • #3
    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 can use something simple which will just store the request like this
    PHP Code:
    <?php
    session_start
    ();
    $_SESSION['url'] = $_SERVER['REQUEST_URI'];
    ?>
    or use a function for a location with a little more information.
    PHP Code:
    <?php
    session_start
    ();
    function 
    curPageURL() {
     
    $pageURL 'http';
     if (
    $_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
     
    $pageURL .= "://";
     if (
    $_SERVER["SERVER_PORT"] != "80") {
      
    $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
     } else {
      
    $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
     }
     return 
    $pageURL;
    }
    $_SESSION['url'] = curPageURL();
    ?>
    The function about will give you http://, the host name, the port, and the current file requested. You could also use $_SERVER['HTTP-REFERRER'] however this may not always be provided because of the browser.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #4
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks so much, how come it's giving me this error...

    Undefined index: HTTPS in /Applications/MAMP/htdocs/test.php on line 4

  • #5
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,638
    Thanks
    2
    Thanked 404 Times in 396 Posts
    The HTTPS index won't always be there. Try something like this:
    PHP Code:
    session_start();

    function 
    currentPageURL()
    {
        
    $isHTTPS = (array_key_exists('HTTPS'$_SERVER) && !empty($_SERVER['HTTPS']));
        
    $hostname $_SERVER['SERVER_NAME'];
        if ((
    $isHTTPS && $_SERVER['SERVER_PORT'] != 443) || (!$isHTTPS && $_SERVER['SERVER_PORT'] != 80))
            
    $hostname .= ':' $_SERVER['SERVER_PORT'];
        
        return (
    $isHTTPS 'https://' 'http://') . $hostname $_SERVER['REQUEST_URI'];
    }
    $_SESSION['url'] = currentPageURL(); 

  • #6
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks, another problem i'm having is that on another page, it's saying that session is not defined. even though the session is started. how do I make the function global so the session variable can be used on any page?

  • #7
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,638
    Thanks
    2
    Thanked 404 Times in 396 Posts
    You have to start the session on each page you need to use session variables. If you have a common include on your pages, a config file for instance, you could start the session there. You can also set the PHP configuration variable session.auto_start or use auto_prepend_file to specify a file to start it.

  • #8
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I am, does it matter if the files are in different directories?

  • #9
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,638
    Thanks
    2
    Thanked 404 Times in 396 Posts
    No, not by default.

  • #10
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    the problem is, I use a file to verify the username and password (verify_user.php), and then if true redirect back to the previous page is what I want.... but the function just redirects to the verification file...

  • #11
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,638
    Thanks
    2
    Thanked 404 Times in 396 Posts
    Quote Originally Posted by gerbera View Post
    the problem is, I use a file to verify the username and password (verify_user.php), and then if true redirect back to the previous page is what I want.... but the function just redirects to the verification file...
    Explaining how it should work without posting the code that is not functioning correctly isn't going to get your problem solved.


  •  

    Posting Permissions

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