• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 263
  • Last Modified:

May a SESSION pass a header and let it do a redirect

In order to shorten the steps needed to login I would like in some instances to create a session that carries a header.
Then on the landing page make sure that the header does its job in re-routing to its page of origin.
Another word
page_aa.php or for that purpose many other pages could be only accessed upon login. I need to make sure that when login is successful the user is automatically sent back where he/she comes from without clicking his/her way back to where the call for login was originated.
Or is there another way to do that automatically?
NB) Why I do not wish letting the user clicking his/her way back:
The reason is that it is a large and complicated site with so many options that call for login. and when logged in a CP is accessed and from the CP one needs to click his/her way back, in some instance I need to bypass the CP and reroute the user where he/she comes from
0
array007
Asked:
array007
  • 2
1 Solution
 
cevomanCommented:
The best way i can think of doing this (as the way i've done this in the past) is either to drop a base64 encoded cookie on the clients machine or if you fear they don't have cookies enabled get PHP to generate a Unique ID based on the users browser, IP etc and dump in a sql database

for exmple

User visits Page 1:
fills in all the information available and clicks submit (submits to page 2)

Page 2: Requires login
Cycles through $_GET and $_POST and dumps the encoded string into a cookie with 2minute life.
User logs in and redirects to page 3

Page 3:
Confirms login,
checks for dumped information,
if found posts information back to page 2 which will now continue as normal because user has a session

Hope that makes sense

Kind regards
James

0
 
array007Author Commented:
it makes sense thanks, but still calls for a few user's manip
what I am looking for is: If login's OK then the user is done and is redirected where he/she comes from.
Kind regards
0
 
Hube02Commented:
This is how I handle this.

Say for instance someone lands on /users/needs_login.php

You detect that the user needs to login and redirect to the login page, but before this you save the current page to a session variable. I'm assuming that you are not worried about cookies at this point because most sites where you login require cookies and sessions also require cookies. I'm also going to check to see if there are any query parameters in the URL, just in case, so we can save these as well.

Below that is the code to retrieve the page and do a redirect.


  // to set return page
  
  $page = $_SERVER['PHP_SELF'];
  
  if (count($_GET) > 0) {
    $query = '?';
    foreach($_GET as $key => $value) {
      if ($query != '?') {
        $query .= '&';
      }
      $query .= $key.'='.$value;
    }
    $page .= $query;
  }
  
  $_SESSION['return_page'] = $page
  
  // to redirect to return page
  // after login is suuccessful
  
  if (isset($_SESSION['return_page'])) {
    $page = $_SESSION['return_page'];
    unset($_SESSION['return_page']);
    header('location: http://'.$_SERVER['HTTP_HOST'].$page);
    exit;
  } else {
    // do your normal redirect here
  }

Open in new window

0
 
array007Author Commented:
Thanks, this is what I was looking for, not tested yet, but if needed I may debug it
kind regards
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now