condition for session changing head.php - php

i am currently working on my index.php, what i want to do is when the user logs in he will be redirected to index.php again but this time with a different head.php to store session of the user.
i have tried this one.
if(isset($_SESSION['isCustomer'])){
include 'includes/customer.head.php';
echo "hahaha";
}
else{
include 'includes/head.php';
}
and here is my checklogin.php
if($row['type'] == 'admin'){
$_SESSION['isAdmin'] = true;
header("location: admin/admin.php");
} else if($row['type'] == 'customer'){
$_SESSION['isCustomer'] = true;
header("location: ../index.php");
}

When you say "i have tried this one."
if(isset($_SESSION['isCustomer'])){
include 'includes/customer.head.php';
echo "hahaha";
}
else{
include 'includes/head.php';
}
I'm guessing it didn't work. Make sure you have
session_start();
at the top of the script. You're condition looks right.

Related

ERR_TOO_MANY_REDIRECTS in PHP

My logout.php file is like this. Is there any mistake in my code
logout.php
<?php
session_start();
session_destroy();
header('Location:index.php');
exit;
?>
Here is my index.php file. If I am set $_SESSION['s_activId'] then it is working properly but when I am trying to put condition if $_SESSION['s_activId'] is not set at that time I want to pass header on index page sometimes it works sometimes it does not work.
<?php
include('include/config.inc.php');
if(!isset($_SESSION['s_activId']))
{
$_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
header("Location:index.php");
}
else
{
$wrong = '';
if(isset($_POST['submit']))
{
$checkLogin = "SELECT userName,password,userType
FROM user
WHERE BINARY userName = '".$_POST['userName']."'
AND BINARY password = '".$_REQUEST['password']."'";
$checkLoginresult = mysql_query($checkLogin);
if($userLoginRow = mysql_fetch_array($checkLoginresult))
{
$_SESSION['s_activId'] = $userLoginRow['userName'];
$_SESSION['s_password'] = $userLoginRow['password'];
$_SESSION['hg_userType'] = $userLoginRow['userType'];
if(!$_SESSION['s_urlRedirectDir'])
{
header("Location:index.php");
}
else
{
header("Location:reminder.php");
}
}
else
{
$wrong = "UserId And Password Is Not Valid";
}
}
}
include("bottom.php");
$smarty->assign('wrong',$wrong);
$smarty->display("index.tpl");
?>
The problem arise in the condition below in index.php:
if(!isset($_SESSION['s_activId']))
{
$_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
header("Location:index.php");
}
When you logout, you are calling session_destroy() on logout.php and redirecting on index.php and the condition above gets true as s_activId is not set in session and again you are redirecting on index.php (without setting s_activId in session). The above condition will be true until the variable s_activId set in session and because of this you are getting ERR_TOO_MANY_REDIRECTS error.
The solution is, on index.php set the variable s_activId in session before calling the header method. Refer the code below:
if(!isset($_SESSION['s_activId']))
{
$_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
$_SESSION['s_activId'] = true;
header("Location:index.php");
}
Dont redirect index.php to index.php. you having redirects loop. Also
if you have code below that also can fire add die in if because after
redirect code below still executes. I didnt read your code, maybe
there isnt problems with this but after
header("Location: lalala"); always add die(); or exit();

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

PHP Session Variable disappears after page refresh

I have a big problem and i cant solve it. My session variables are exchanged between files but after refresh of the second page they disappear.
Here's the code:
index.php
session_start();
header('Title: So random');
header('charset: UTF-8');
//if index.php?login is requested
if(isset($_REQUEST['login'])) {
//'pass' input box value (from POST) is saved to $pass variable.
$pass = $_POST['password'];
//if pasword matches Password.
if($pass == 'Password') {
//session_start();
$_SESSION['logintoken'] = "approoved";
header("Location: list.php");
die();
} else { $error = true; }
}
if(isset($_SESSION['logintoken'])) {
header('Location: list.php');
die();
}
?>
Random HTML With login page goes here...
And then we have page, which is availble only for logged in. After redirecting from login to it it's okay but after refresh i have "logintoken not defined".
list.php
<?php
session_start();
if($_SESSION['logintoken'] != "approoved") {
//'<meta http-equiv="REFRESH" content="0; url=index.php">'
die();
}
?>
<html> goes here....
header('Title: So random');
header('charset: UTF-8');
$_SESSION['logintoken']='' ;
EDIT:
perhaps this is better
if(isset($_SESSION['logintoken']) && ($_SESSION['logintoken'] != "approoved"))
EDIT 2:
header("Location: list.php?token=".$_SESSION['logintoken']);
list.php
if($_REQUEST['token'] != "approoved") {
//'<meta http-equiv="REFRESH" content="0; url=index.php">'
die();
}

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