Solved

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

Posted on 2009-05-14
4
233 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks, this is what I was looking for, not tested yet, but if needed I may debug it
kind regards
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

772 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now