Jump to content

[SOLVED] set a session when you use header redirect?


aebstract

Recommended Posts

You can, create an "overrided" header function. Just make sure you put this in a file that is included in every page:

 

function header_($header, $setPage=true) {
     if ($setPage) 
         $_SESSION['last_page'] = $_SERVER['PHP_SELF'];

     header($header);

     return true;
}

 

Then just use that, which it should set the session variable and viola.

Link to comment
Share on other sites

That is checking to see if the header is being ran, period. Right? I'm gonna need something a tad more specific, if the header being ran is header("Location: index.php?page=login");

 

So I'm gonna need something like

 

function header_(Location: index.php?page=login, $setPage=true) {
     if ($setPage) 
         $_SESSION['last_page'] = $_SERVER['PHP_SELF'];

     header(Location: index.php?page=login);

     return true;
}

? Or I'm not sure I understand it :(

Link to comment
Share on other sites

? Or I'm not sure I understand it :(

 

Let me explain with an example.

 

function header_($header, $setPage=true) {
     if ($setPage) 
         $_SESSION['last_page'] = $_SERVER['PHP_SELF'];

     header($header);

     return true;
}

 

That is added in some file that is included.

 

<?php
session_start();
include('functions.php'); // has our header function.

if (!$_SESSION['loggedin']) {
    header_("Location: index.php?page=login");
}
?>

 

Simple as that, now the session contains ['last_page'] in it, the user is redirected where you want them to be and you can then redirect them (with or without using header_) to the page they were trying to access.

 

Link to comment
Share on other sites

Ok so it is set on the index page. how about the page that is being called? Is this a separate page or does it use the index page as a gateway?

 

session_start has to be at the top of any page you want to use sessions on. 

 

EDIT:

And yes, please do as Thorpe suggested :)

Link to comment
Share on other sites

well ya can make that whole process easier, just work on the idea of how it shud run.

It sounds like u have some pages ya want to protect, and others are public.

well than ya needs a way to let yer script know which pages are what.

 

file: protect.php

<?php
session_start();
if(!isset($_SESSION['loggedin']) && !defined(PROTECTED))
{
   $_SESSION['last_page'=$_SERVER['PHP_SELF']
   header("Location: login.php");
   exit;
}

 

file: unprotected.php

<?php
// Sample Unprotected Page
?>
<H1>UnProtected</H1>

include("inlcude.php");

[/code]

 

file: protected.php

<?php
define('PROTECTED',TRUE);
include("inlcude.php");
?>
<H1>Protected</H1>

 

 

good luck

 

Link to comment
Share on other sites

all other pages are included in to index.

 

heres an example, products page:

<?php
if(!isset($_SESSION["id"]))
{
header("Location: index.php?page=login");
exit;
}

 

 

 

 

 

login.php

<?php
ini_set ("display_errors", "1");
error_reporting(E_ALL);
header("Cache-control: private");
if(isset($_SESSION["id"]))
{
header("Location: index.php?page=myaccount");
exit;
}

$error = "";

echo "$_SESSION[last_page]";

 

 

 

index.php

<?php
session_start();
header("Cache-control: private");

require_once('functions.php');

$content = '';

 

 

These are really the only bits that matter, the function is in functions.php

Link to comment
Share on other sites

<?php
ini_set ("display_errors", "1");
error_reporting(E_ALL);
session_start(); // Note session_start added here
header("Cache-control: private");
if(isset($_SESSION["id"]))
{
header("Location: index.php?page=myaccount");
exit;
}

$error = "";

echo "{$_SESSION['last_page']}";

 

<?php
session_start();  // note session_start here as well.
if(!isset($_SESSION["id"]))
{
header("Location: index.php?page=login");
exit;
}

Link to comment
Share on other sites

thorpe, the function that was made earlier in this forum is the one used within functions.php which you can see is included in to index.php

 

premiso, I understand it needs to be at the top, but the function that creates the session isn't on the login page, its in functions.php which is simple a php file of functions created. Needs to go top of that page?

 

Link to comment
Share on other sites

Okay just figured out that this won't work:

 


function header_($header, $setPage=true) {
     if ($setPage)

header("Location: index.php?page=thisworks");

     return true;
}

 

I still go to page=login, whenever I use just that. Like I said before, I don't understand exactly what's going on in this so it's hard for me to really troubleshoot that function.

Link to comment
Share on other sites

thorpe, the function that was made earlier in this forum is the one used within functions.php which you can see is included in to index.php

 

That may be so, but Its still not being called anywhere?

Link to comment
Share on other sites

inside functions.php:


function header_($header, $setPage=true) {
     if ($setPage)
header("Location: index.php?page=thisworks");
         $_SESSION['last_page'] = $_SERVER['PHP_SELF'];
$_SESSION['count_last_page'] = 1;
     header($header);

     return true;
}

 

 

inside index.php:

<?php
session_start();
header("Cache-control: private");

require_once('functions.php');

 

The functions is pulled in at the top of the index..

Link to comment
Share on other sites

He means where are you calling: "header_"  not just "header"

 

You have posted where you defined it. It has to be called.

 

<?php
session_start();  // note session_start here as well.
if(!isset($_SESSION["id"]))
{
header_("Location: index.php?page=login"); // note it is now header_ instead of header
exit;
}

 

Functions have to be called to actually run. You cannot just define them and expect them to run on their own.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.