Solved

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

Posted on 2009-05-14
4
250 Views
Last Modified: 2013-12-12
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
Comment
Question by:array007
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 3

Expert Comment

by:cevoman
ID: 24387691
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
 
LVL 1

Author Comment

by:array007
ID: 24387812
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
 
LVL 18

Accepted Solution

by:
Hube02 earned 500 total points
ID: 24387833
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
 
LVL 1

Author Closing Comment

by:array007
ID: 31581590
Thanks, this is what I was looking for, not tested yet, but if needed I may debug it
kind regards
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question