Session is saved but the page kick me out - php

I tried to check if the session is still exist, if it doesnt, it will kick me out from the page.
session_start();
if (empty($_SESSION['locationed']))
{
header("Location: http://exit.php");
die();
}
The code above always kick me out of the page.
When I delete the code above and echo the session:
$locationed=$_SESSION['locationed'];
<?php echo $locationed; ?>
It echos my session of location.
What is wrong? Help please.

Here you go .
if (!isset($_SESSION['locationed'])) {
header("Location: http://exit.php");
die();
}

try isset rather than empty.
if (!isset($_SESSION))
{
header("Location: http://exit.php");
die();
}

try:
if (isset($_SESSION['locationed']))

Try those combinations,
if (!isset($_SESSION['locationed']) && empty($_SESSION['locationed'])) {
...
}

session_start();
$_SESSION['locationed']="1234";
echo $_SESSION['locationed']; //if echo something means getting something on that
if($_SESSION['locationed'] =='' && !isset($_SESSION['locationed']))
{
header("Location: http://exit.php");
die();
}

session_start();
if(!isset($_SESSION["locationed"]) || empty($_SESSION['locationed']))
{
header("Location: http://exit.php");
die();
}

Related

how to make sure not to read another page only after loging in

here you may find my verification code
with this code i want not to redirect to another page only after logging in
how shall i do ?
<?php
if (isset($_SESSION['login']))
session_start();
if (!isset($_SESSION['login']) || $_SESSION['login'] != 'ok') {
header("location:login.php");
exit();
}
?>
i would use isset combined with empty just for extra precaution
session_start();
if(isset($_SESSION['login']) && !empty($_SESSION['login'])) {
header("location:user.php");
}
else {
header("location:login_form.php");
}
don't use exit(); just redirect

unset variable done before it should unset

I have this peace of code of which I don't understand why it's not working as I expect.
<?php
session_start();
if($_SERVER['REQUEST_METHOD'] == "POST") {
$_SESSION['reg'] = "done";
header("Location: " . SELF, true, 302);
}
if((isset($_SESSION['reg']) AND ($_SESSION['reg'] == "done"))) {
unset($_SESSION['reg']);
echo "Done";
}else{
echo "Not done";
}
?>
After POST it redirects and echo's Not done but I would expect it would echo's Done. If I remove the line with unset, it works fine and echo's Done.
This isn't the behavior I would expect. What mistake am I making?
A header redirect does not stop execution of the script, so the unset is carried out immediately.
To fix exit after the redirect
<?php
session_start();
if($_SERVER['REQUEST_METHOD'] == "POST") {
$_SESSION['reg'] = "done";
header("Location: " . SELF, true, 302);
exit();//<-- add this
}
if((isset($_SESSION['reg']) AND ($_SESSION['reg'] == "done"))) {
unset($_SESSION['reg');
echo "Done";
}else{
echo "Not done";
}
?>
write this unset($_SESSION['reg']); instead of this unset($_SESSION['reg');

php session doesn't work

How it should work:
Index.php is the secured page. It includes check.php, which checks if you have a session = good. If it hasn't, you're not logged in -> log off, remove session. But it doesn't work, it always logs off, like I didn't log in...
index.php
include ‘check.php’;
echo "logged in";
check.php
session_start();
if($_SESSION[‘login’] != ‘good’) {
unset($_SESSION[‘login’]);
unset($_SESSION[‘name’]);
header(‘Location: login.php?logoff’);
exit();
}
Login.php
if(isset($_POST[‘login’])) {
$gb = array();
$gb[‘user1’] = ‘pass1’;
$gb[‘user2’] = ‘pass2’;
if(isset($gb[$_POST[‘username’]]) && $gb[$_POST[‘username’]] == $_POST[‘password’])
{
$_SESSION[‘login’] = ‘good’;
$_SESSION[‘name’] = $_POST[‘name’];
header("Location: index.php");
} else {
header("Location: login.php?wrongpass");
}
} else { ?>
Login Form
<?php } ?>
I hope someone can help me!
You should verify you started the session in login.php.
Put session_start(); in all the pages
You need to have session_start() at the top of all the pages, you havent shown the session start for your login page.
(Thanks to Danny for proving I cant type)
Check that you have register_globals is On in your php.ini
First check on the pages you want to use session variables session is start or not and if session is not stat then start it.
and this is the very first line in the php file.
Code for the session checking is :
if(!session_id())
{
session_start();
}
if($count==1){
session_start();
$_SESSION['Username'] = $UserName;
$_SESSION['Password'] = $password;
UpdateOnlineChecker($Session);
header( "Location: http://". strip_tags( $_SERVER ['HTTP_HOST'] ) ."/newHolo/" );
exit;
}
else {
echo "Wrong Username or Password";
}
Look at my code. It checks if the statement is true (for me, if there is one row with a query statement i execute). Then i start a session and basically Ill define global session variables, sned out a query to my database to update the session and then refer through.
you are missing a session_start(); in your if true block.
Use one for action document such as index.php there is code:
session_start();
if(isset($_POST['login']) && isset($_POST['password'])){
// login
header('Location: (here is some page)');
}
if(!isset($_SESSION['user']){
// #todo some action
} else {
require_once('login.php');
}
if(isset($_GET['logout'])){
unset($_SESSION['user']);
header('Location: (here is some page)');
}
I think problem is header:
('location:------.php);
Your hosting server doesn't run this.
You can use this:
echo "<script>window.location.href='-----.php'</script>";

PHP Redirect with Cookies

How do I redirect to a splash page once with cookies?
I'm setting the cookie on my splash.php page to this:
<?php
$expire = time()+60;
setcookie("no_splash", "1", $expire);
?>
On that page there's a link to my index.php with this:
<?php
if($_COOKIE['no_splash']== '1') {
header("Location: index.php");
echo "it works";
} else if($_COOKIE['no_splash']!= '1') {
header("Location: splash.php");
};
?>
I keep getting a redirect loop error but can't figure why.
You are redirecting to index.php from the index.php file, hence the loop.
Change your code to be simply
if($_COOKIE['no_splash'] != '1') {
header("Location: splash.php");
exit;
}
or indeed
if(!$_COOKIE['no_splash']) {
header("Location: splash.php");
exit;
}
which is the same thing.
$expire = time()+60;
header("Set-Cookie: no_splash=1; expires=$expire; path=/");
header("Location: index.php");
Have you tried simply:
<?php
if($_COOKIE['no_splash']== '1') {
echo "it works";
} else {
header("Location: splash.php");
};
?>
Maybe isset($_COOKIE['no_splash']) rather than `$_COOKIE['no_splash']== '1'?
Also, not sure it this is what you want, but you can set simply not set the expiration time (or set it to 0), and it will delete the cookie when the browser is closed, so if they keep it open, they won't have to go back to the splash.

PHP Redirect Loop

On my index page I have a link to my login.php page with this code:
<?php
if(isset($_SESSION['username'])) {
echo "<div id='logout'><a href='logout.php'>Logout (".$_SESSION['username'].")</a></div>";
} else {
echo "<div id='login'><a href='login.php'>Login (Regular)</a></div>";
}
?>
On the login.php page I have
<?php
include('check.php');
$ref = getenv('HTTP_REFERER');
if (isset($ref)) {
header("Location: " . $ref);
exit;
} else {
header("Location: index.php");
exit;
}
?>
check.php is the code for the login form and it checks the users level to make sure they can access the page. I was told that I need to add a check to see if the referral is login.php, otherwise it will go in an infinite loop and I am of course getting "This webpage has a redirect loop". However, I have no clue how to do this and I can't find any information on how to fix it. Anyone know a quick solution?
You should be able to just do
if (isset($_SERVER['HTTP_REFERER']) && end(explode('/',$_SERVER['HTTP_REFERER'])) != 'login.php') {
header("Location: " . $_SERVER['HTTP_REFERER']);
exit;
} else {
header("Location: index.php");
exit;
}
Note that this is a simplified code - you may need to be a bit smarter than that.

Categories