Solved

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

Posted on 2009-05-14
4
246 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

749 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