Jump to content

redirect user after log in


jose07

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/224158-redirect-user-after-log-in/
Share on other sites

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;
}

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.