barbs75 Posted July 11, 2008 Share Posted July 11, 2008 Hey guys, The problem i am having is with navigating back to a previous page in php... What i am trying to do is, when a user tries to go to a certain page, they have to be logged in, otherwise they can't access it. So what i do is check that the user is logged on, if they are not then they are directed to my login page where they can log in. But in my script i have a header which directs them to the profile page, but i dont want them to go there, i want them to be directed back to the page that they were TYRING to access.... I really can't figure out how to do this.....searched google and stuff too, but nothing comes up in the example i am trying to do.. Can anyone give me insight on how to do this?? cheers Craig Quote Link to comment https://forums.phpfreaks.com/topic/114310-returning-to-previous-page/ Share on other sites More sharing options...
DarkWater Posted July 11, 2008 Share Posted July 11, 2008 Store it in the URL when you redirect them to the login page. I've seen sites with like: login.php?redirect=somepage.php Quote Link to comment https://forums.phpfreaks.com/topic/114310-returning-to-previous-page/#findComment-587791 Share on other sites More sharing options...
.josh Posted July 11, 2008 Share Posted July 11, 2008 inside your condition that checks if user is logged in, before the header redirect, do $_SESSION['currentpage'] = 'page.php'; or $_SESSION['currentpage'] = $_SERVER['PHP_SELF']; and then in your login script, where you send them to the default logged in page upon successful login, before the header redirect, do $location = ($_SESSION['currentpage'])? $_SESSION['currentpage'] : "profile.php"; header("Location: $location"); edit: or yeah, you can pass it via GET method like darkwater mentioned Quote Link to comment https://forums.phpfreaks.com/topic/114310-returning-to-previous-page/#findComment-587796 Share on other sites More sharing options...
DarkWater Posted July 11, 2008 Share Posted July 11, 2008 Yeah, I was thinking that too. It would work. Quote Link to comment https://forums.phpfreaks.com/topic/114310-returning-to-previous-page/#findComment-587797 Share on other sites More sharing options...
discomatt Posted July 11, 2008 Share Posted July 11, 2008 What? Remove the header redirect to the profile page... or simply have if ( $loggedOn == TRUE ) header( 'Location: profile.php' ); else header( 'Location: login.php' ); Store it in the URL when you redirect them to the login page. I've seen sites with like: login.php?redirect=somepage.php A cleaner way would be to use sessions to track the previous page the user has been on, I do this all the time. At the bottom of my scripts i have $_SESSION['lastPage'] = $_SERVER['REQUEST_URI']; Quote Link to comment https://forums.phpfreaks.com/topic/114310-returning-to-previous-page/#findComment-587798 Share on other sites More sharing options...
DarkWater Posted July 11, 2008 Share Posted July 11, 2008 Yeah yeah, I realized sessions right after I posted and I didn't feel like editing because I knew someone would suggest it. =P Quote Link to comment https://forums.phpfreaks.com/topic/114310-returning-to-previous-page/#findComment-587803 Share on other sites More sharing options...
the shig Posted July 11, 2008 Share Posted July 11, 2008 could use <?php // login.php $page_just_on = $_SERVER['HTTP_REFERER']; $query_string = $_SERVER['QUERY_STRING'] != '' ? '?' . $_SERVER['QUERY_STRING'] : ''; $redirect_page = $page_just_on . $query_string; ?> store $redirect_page as a hidden input field in the login form. then after processing the login and checking their details, just redirect to the page. Quote Link to comment https://forums.phpfreaks.com/topic/114310-returning-to-previous-page/#findComment-587811 Share on other sites More sharing options...
DarkWater Posted July 11, 2008 Share Posted July 11, 2008 HTTP referrers are not reliable. Quote Link to comment https://forums.phpfreaks.com/topic/114310-returning-to-previous-page/#findComment-587817 Share on other sites More sharing options...
the shig Posted July 11, 2008 Share Posted July 11, 2008 HTTP referrers are not reliable. didn't know this. Also, imagine link from website A to website B login form using the referer method. login... then get redirected back to website A. wtf catastrophe Quote Link to comment https://forums.phpfreaks.com/topic/114310-returning-to-previous-page/#findComment-587868 Share on other sites More sharing options...
unsider Posted July 11, 2008 Share Posted July 11, 2008 I use this method.. Even though it's been discussed in a way, just giving practical examples if(isset($_SESSION['url'])){ $this->referrer = $_SESSION['url']; }else{ $this->referrer = "/"; } Then call the referrer from outside the class... i.e $session->referrer Although I've only recently started using this method, so I don't know how effective it is, or reliable, etc.. If you guys figure out a better waty, let me know Quote Link to comment https://forums.phpfreaks.com/topic/114310-returning-to-previous-page/#findComment-587871 Share on other sites More sharing options...
barbs75 Posted July 11, 2008 Author Share Posted July 11, 2008 hey guys, cheers for your overwhelming response!! you were all a great help thankyou!! I'm having a slight problem now to do with directories.... i have my login script within a folder called 'function' so when the login page is called, it goes: /function/login.php so when i try redirecting to a page not in this directory it can't find it, because it isn't the same directory as the login script, say if i was trying to redirect to index.php it would do the following: /function/index.php does anyone know how i go back through a directory?? cheers for your help guys Craig Quote Link to comment https://forums.phpfreaks.com/topic/114310-returning-to-previous-page/#findComment-587974 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.