jose07 Posted January 12, 2011 Share Posted January 12, 2011 Hello, I'm having trouble trying to redirect users after they've logged in. My site shows different types of adverts but some adv are restricted to registered users, on the home page they can see the the title of the adv but if they click on the title and are not logged in then they are taken to the log in page, Now my problem is how do i then re-direct them back to the adv they were trying to see? if i just redirect them to index.php that won't help because index shows few adv. I hope that makes sense. Any help much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/224158-redirect-user-after-log-in/ Share on other sites More sharing options...
dolrichfortich Posted January 12, 2011 Share Posted January 12, 2011 First, when they have reached a page that is restricted, save first the url in a session before you redirect. $_SESSION['redirect_url'] = URL_OF_THE_PAGE; Here is a site which has a tutorial on getting the current url of a page. http://www.webcheatsheet.com/PHP/get_current_page_url.php Once they successfully logged in, check first if there is a redirect variable in the session. If there is a redirect variable, get the variable first, unset the redirect_url session, then redirect using the variable. If the redirect_url is not set, just redirect directly to index.php if(isset($_SESSION['redirect_ul'])) { $redirect_url = $_SESSION['redirect_ul']; unset($_SESSION['redirect_ul']); header('Location: ' . $redirect_url); exit; } else { header('Location: index.php'); exit; } Quote Link to comment https://forums.phpfreaks.com/topic/224158-redirect-user-after-log-in/#findComment-1158279 Share on other sites More sharing options...
jose07 Posted January 18, 2011 Author Share Posted January 18, 2011 Thanks for your reply, but i'd like some more explanation because unfortunately i couldn't get it to work. On the $_session['redirect_url']= here should i have a variable i.e the name of the function that gets the site address?(because the address is generated dynamically from db) also it seems to just take me straight to the index page? should i declare the session on every page? Headers is there an alternative as i get the error 'headers already sent' and if i use ob_start() it block the metadata from being published is there a way around this? Thanks a lot for your help so far Quote Link to comment https://forums.phpfreaks.com/topic/224158-redirect-user-after-log-in/#findComment-1161130 Share on other sites More sharing options...
crabfinger Posted January 18, 2011 Share Posted January 18, 2011 modifying this post Quote Link to comment https://forums.phpfreaks.com/topic/224158-redirect-user-after-log-in/#findComment-1161208 Share on other sites More sharing options...
Pikachu2000 Posted January 18, 2011 Share Posted January 18, 2011 Post the code you tried. Quote Link to comment https://forums.phpfreaks.com/topic/224158-redirect-user-after-log-in/#findComment-1161410 Share on other sites More sharing options...
Kryllster Posted January 18, 2011 Share Posted January 18, 2011 Have you tried header("Location:page_to_goto.php"); Quote Link to comment https://forums.phpfreaks.com/topic/224158-redirect-user-after-log-in/#findComment-1161418 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.