Session Redirect

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Session Redirect

Post by AliasBDI »

Is it possible to redirect someone who has no session to register? I have a page that demands "session_register()" at the beginning, but I want it to check and IF they do not have one (or have not logged in), it will redirect them to a page to login. Plus it will need to pass the URL that they originally were attempting so that they can pick up where they left off?

Surely this is possible. Any ideas?
madpixel
Forum Newbie
Posts: 4
Joined: Wed Oct 22, 2003 4:31 am

redirect

Post by madpixel »

you can redirect like this :

Code: Select all

<?php
if(!session_is_registered('user'))
{
	header("location:login.php");
	exit;
}
?>
User avatar
Kriek
Forum Contributor
Posts: 238
Joined: Wed May 29, 2002 3:46 am
Location: Florida
Contact:

Post by Kriek »

Code: Select all

<?php
    ob_start();
    $dom = $_SERVER&#1111;'SERVER_NAME'];
    if(!headers_sent()) &#123;
        session_start();
        if(empty($_SESSION&#1111;'user'])) &#123;
            session_write_close();
            header('Location: http://' . $dom . '/login.php');
            exit();
        &#125;
    &#125;
    ob_end_flush();
?>
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Got it

Post by AliasBDI »

Got it working.... I had to add the "else" clause to it though. But it is working dandy. Now I need it to carry the URL so that when the person logs in, it redirects them to the URL they originally wanted.

Know how?
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Anybody?

Post by AliasBDI »

Anybody know how to carry the URL or URL variables from page to page so that after a user has logged in, then they can pick up where they leave off?
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Try this, and rearrange to fit you.

Code: Select all

echo 'http://'. $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

more help...

Post by AliasBDI »

I have to go back on my word. I seem to have never gotten the "if no session, then redirect" script.

Does "if(!session_is_registered('user'))" check for the session named "user"? Because I am not establishing a name for the sessions. I use "session_start()" and "session_register()".

Here is my code:

Code: Select all

<?php 
if(!session_is_registered('user')) 
&#123; 

// declare link as variable
$redirect = 'http://'. $_SERVER&#1111;'SERVER_NAME'] . $_SERVER&#1111;'REQUEST_URI'];

include 'login.php';
   exit; 
&#125; 
else
&#123;
session_register(); 
require_once("../includes/gather.php");
&#125;
?>
Any ideas?
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Edited:
Server configuration dependent. I had addons and hacks that made this possible. Should be ignored.
Last edited by JAM on Mon Oct 27, 2003 8:53 pm, edited 1 time in total.
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

JAM

Post by AliasBDI »

Did not work for me. :(
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

You can bypass this by setting a dumb session variable. If $_SESSION does not work, you are simply having an PHP version less than 4.2.

Code: Select all

<?php
    session_start();
    if (isset($_SESSION['loggedin'])) {
        echo 'Logged in';
    } else {
        $_SESSION['loggedin'] = 1;
        echo 'Not logged in';
        exit;
    }
?>
Post Reply