Page 1 of 1
Session Redirect
Posted: Wed Oct 22, 2003 7:56 am
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?
redirect
Posted: Wed Oct 22, 2003 8:04 am
by madpixel
you can redirect like this :
Code: Select all
<?php
if(!session_is_registered('user'))
{
header("location:login.php");
exit;
}
?>
Posted: Wed Oct 22, 2003 8:46 am
by Kriek
Code: Select all
<?php
ob_start();
$dom = $_SERVERї'SERVER_NAME'];
if(!headers_sent()) {
session_start();
if(empty($_SESSIONї'user'])) {
session_write_close();
header('Location: http://' . $dom . '/login.php');
exit();
}
}
ob_end_flush();
?>
Got it
Posted: Wed Oct 22, 2003 8:47 am
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?
Anybody?
Posted: Mon Oct 27, 2003 1:37 pm
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?
Posted: Mon Oct 27, 2003 5:41 pm
by JAM
Try this, and rearrange to fit you.
Code: Select all
echo 'http://'. $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
more help...
Posted: Mon Oct 27, 2003 7:44 pm
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'))
{
// declare link as variable
$redirect = 'http://'. $_SERVERї'SERVER_NAME'] . $_SERVERї'REQUEST_URI'];
include 'login.php';
exit;
}
else
{
session_register();
require_once("../includes/gather.php");
}
?>
Any ideas?
Posted: Mon Oct 27, 2003 8:17 pm
by JAM
Edited:
Server configuration dependent. I had addons and hacks that made this possible. Should be ignored.
JAM
Posted: Mon Oct 27, 2003 8:43 pm
by AliasBDI
Did not work for me.

Posted: Mon Oct 27, 2003 9:01 pm
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;
}
?>