Redirect in PHP regarding on session variable - php

when someone visit "other page" is redirected on index.php,
but when after that tries to open "other page" again is redirected.
Please help, because the code below doesnt work.
on page index.php
<?php
session_start();
$_SESSION['INDEX']="1";
?>
on all "other pages"
<?php
if($_SESSION['INDEX']!="1"){
header("Location: ../index.php");
}
?>

Add session_start(); at the top of other pages.

You need to add session_start(); on all pages where need to check if any session value exist.
change your code:
<?php
if($_SESSION['INDEX']!="1"){
header("Location: ../index.php");
}
?>
with
<?php
session_start();
if($_SESSION['INDEX']!="1"){
header("Location: ../index.php");
}
?>

Related

How to create a log-out link inside another php page

I want to include a link to my logout.php which includes code to destroy the session inside my php on this page how would I do this?
<?php
if (isset($_SESSION['username'])) {
echo 'Welcome';
"Logout";
} else {
include 'loginform.php';
echo 'Please Log In';
}
?>
Can you try this code, if the SESSION is empty:
<?
session_start();
if (empty($_SESSION['username'])){
echo 'Please Log In';
include 'loginform.php';
}else{
echo 'Welcome';
?>
Home
<?
}
?>
Before you use any $_SESSION variable, you have to call session_start()
The logout should first open the session by calling session_start(), then destroy it by calling session_destroy
You aren't currently telling PHP to display the link to the logout page. It either needs to be included in the echo or outputted elsewhere. For example;
if (isset($_SESSION['username'])) {
echo 'Welcome';
echo 'Logout';
}
else {
include 'loginform.php';
echo 'Please Log In';
}
In your logout.php put this code ,
<?php
session_start();
session_destroy();
?>

PHP Header redirect and echo out session on next page?

i am submitting a form and then using header redirect to take the user to a new page. how can i add a session to my header redirect to say once user has been redirected echo out a div within a session saying something like form submitted?
heres what i have tried to do but can not get it to work, can someone please point me in the right direction, thanks.
submit_form.php:
header("Location: ../index.php?success=$success");
index.php:
<?php echo $_SESSION['success']; ?>
<?php $success= "<div> CONGRATULATIONS!!!!!</DIV>"; ?>
A session value is something stored in a session started with session_start().
What you have is a URL query parameter, which you can access with $_GET['success'].
submit_form.php:
session_start();
$_SESSION['success'] = true;
header("Location: ../index.php?success=$success");
index.php:
session_start();
if (isset($_SESSION['success']) && $_SESSION['success']) {
//Echo your div
}
You appear to be mixing up $_SESSION and $_GET
Try below code:
On submit_form.php page:
$_SESSION['success'] = "YOUR SUCCESS MESSAGE";
header("Location: ../index.php");
On index.php page:
if(isset($_SESSION['success']) && $_SESSION['success']!=""){
echo $_SESSION['success'];
unset($_SESSION['success']);
}
On both page, on top, put below code:
session_start();

how to redirect to root folder after logout?

i am in this address in my project
localhost/v1.2/login_pane/login_pane-personnelselect/profile/profile.php
and i want to redirect to this page [localhost/v1.2/login_pane/loginmain.html
and the page always redirect me to localhost/v1.2/login_pane/login_pane-personnelselect/login_pane/index.html
i have this php code in my logout.php but it doestnt work
<?php
session_start();
if(session_destroy())
{
header("Location: ../../loginmain.html");
}
?>
how will i able to make it?
Try Like this
<?php
session_start();
if(session_destroy())
{
header("Location: ../../loginmain.html");
}
?>
try this
<?php
session_start();
session_unset();
header("Location: ../../loginmain.html");
?>
Please have try with following code:
session_start();
unset($_SESSION["nome"]); // where $_SESSION["nome"] is your own variable. if you do not have one use only this as follow **session_unset();**
header("Location: ../../loginmain.html");

Session is not maintained

I have a simple authentication: you login in the login.php page and you are redirected to the home.php page.
This is the code of login.php:
if(pg_num_rows($rs) == 0){ //I search in db for a row with username and password
$errMess = "error";
pg_close($conn);
}else{
$row = pg_fetch_row($rs);
session_start();
$_SESSION['username']=$_POST["nick"];
$_SESSION['admin'] = $row[0];
pg_close($conn);
header("Location: /home.php");
}
now in the home I have the header done in this way:
<?php require_once("scripts/functions.php");
require_once("scripts/config.php");
session_start();
?>
<div id="siteHeader" class="headersLeft"><?php echo WELCOME;?></div>
<div id="userContainer" class="headersRight">
Logged as: <?php echo getDisplayName(); ?>
<?php if(isset($_SESSION['username'])) {?>
<button class="button" onclick="location.href='/logout.php';">logout</button>
<?php }else{ ?>
<button class="button" onclick="location.href='/login.php';">login</button>
<?php }
?>
</div>
it doesn't work: even if data is correct it still gives me "guest", the session variable is lost in the header passage..how come?
Solved: i was under windows and the default path to the temp folder, where php actually saves session files, was wrong: was "/tmp" and was not recognized.
I set it to "C:\php\tmp" and it worked: session file was not saved at all!
Write session_start(); on top of everything (right after
<?php
session_start();
require_once("scripts/functions.php");
require_once("scripts/config.php");
?>
or if still doesn't work then write your code like this:
<?php
ob_start();
session_start();
require_once("scripts/functions.php");
require_once("scripts/config.php");
?>
Also don't forget to put these two lines at the top of your login.php page.
Hope it helps :)
I'm guessing there's some more code after the if statement that continues to manipulate $_SESSION. That's where $_SESSION['username'] is assigned the 'guest' value.
Remember, header("Location: /home.php"); only sets a response header. It doesn't redirect immediately, stopping script execution.
Place a exit; command right after header() to prevent execution from reaching the rest of the code:
header("Location: /home.php");
exit;
this works for me:
session_save_path ( "" ) ;
session_start();

how to use multiple headers in php with if statement

i want to redirect login.php to index.php when $_SESSION['user'] is not empty (user logged in)
<?php
session_start();
if (isset($_SESSION['user'])){
header ('refresh:3 ; URL:index.php');
}
?>
but when user log in the page doesn't redirect to the index.php
This should work:
<?php
session_start();
if (isset($_SESSION['user'])){
header('Location: http://www.yoursite.com/');
die();
}
?>
If you want to redirect the user after x senconds, then use
<?php
session_start();
if (isset($_SESSION['user'])){
header( "refresh:3;url=whatever.php" );
}
?>
You're doing it wrong. Example of how to do it and some more info about the header.
<?php
session_start ();
if (isset($_SESSION['user'])
{
header ('Refresh: 3; url=index.php');
// ^
}
?>
You used : it should be an equal sign.

Categories