Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2009-05-14
4
Medium Priority
?
257 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
  • 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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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 dynamically set the form action using jQuery.
Suggested Courses

971 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