Session Redirect
Moderator: General Moderators
Session Redirect
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?
Surely this is possible. Any ideas?
redirect
you can redirect like this :
Code: Select all
<?php
if(!session_is_registered('user'))
{
header("location:login.php");
exit;
}
?>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
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?
Know how?
Try this, and rearrange to fit you.
Code: Select all
echo 'http://'. $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];more help...
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:
Any ideas?
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");
}
?>Edited:
Server configuration dependent. I had addons and hacks that made this possible. Should be ignored.
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.
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;
}
?>