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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.