global a $_SESSION variable - php

I'm trying to make a login session on PHP, but it appears that the $_SESSION['username'] dies inside the IF sentence (I thought $_SESSION where globals by default), and I cant echo it out of the IF
heres My code
if($name=="admin" && $password=="admin")
{
session_start();
$_SESSION['username'],$_SESSION['sesion'];
$_SESSION['username']=$name;
$_SESSION['sesion']=1;
echo $_SESSION['username'];
echo "<br>";
echo $_SESSION['sesion'];
}
echo "<br>";
echo $_SESSION['username'];
The last echo doesnt print its VALUE, So when I redirect it to another page, the page doesnt take the username value
I'm kind of new in this matter
So dont be so harsh on me :P
How can I do this??

Move session_start() to the top of the file:
// foo.php
<?php
session_start();
//....
if($name=="admin" && $password=="admin")
{
// $_SESSION['username'],$_SESSION['sesion']; // Remove this line
$_SESSION['username']=$name;
$_SESSION['sesion']=1;
echo $_SESSION['username'];
echo "<br>";
echo $_SESSION['sesion'];
}
echo "<br>";
echo $_SESSION['username'];

When You are using sessions in php You must start it with session_start() in every file/page You want to get or set session values, so just add this line to this file and that file You're redirecting to.

session_start() should be above the if statment. best to put it right below the ?php

Related

how to pass session value other page if condition true in php

i have a $_sesstion['usermail']. i want to pass this value to next page.if condition match ($answer= $_SESSTION['usermail']);
if(isset($_POST['compair']))
{
echo $_SESSION['question'];
$_SESSION['usermail'];
$answer=$_POST['answer'];
if ($answer == $_SESSION['answer'])
{
header("Location:resetpass.php");
}
else
{
echo "<script>alert('Please Try again')</script>";
}
}
i want to pass $_sesstion['usermail'] value on resetpass.php page.
I think your logic is wrong here. What exactly are you checking in the if statement. A session variable means you can use it on every page that has session_start(); on top.
Sessions by default pass to other pages.
Make sure you have start_session(); on top of the page you want to access the session variable.
So if $_SESSION['usermail'] is working on your current page, it'll work on your next as well with same data.
Get an idea from this exmple
First Page
<?php
session_start();
$_SESSION['name'] = "Adam";
?>
Second page
<?php
session_start();
echo $_SESSION['name'];
?>
You can use GET methods for sharing your session value to next page...
if(isset($_POST['compair']))
{
echo $_SESSION['question'];
$_SESSION['usermail'];
$answer=$_POST['answer'];
if ($answer == $_SESSION['answer'])
{
$value_to_share=$_SESSION['usermail']; // You can share using GET
header("Location:resetpass.php?value=$value_to_share");
// receive this value at resetpass.php by $_GET['value']
}
else
{
echo "<script>alert('Please Try again')</script>";
}
}

php SESSION gets destroyed after redirection

I have a page where I set a value to a SESSION but when I redirect to another page
ex. index.php that value I put to that SESSION doesn't exist anymore!
<?php
session_start();
// this is the page where I set a value to a SESSION called var!
$SESSION['var'] = "hello";
if(isset($SESSION['var'])){
echo "Yes it is";
header("location: test.php");
exit();
}
else {
echo "No it isnt";
}
?>
And this is the test.php where I get the SESSION undefined error!
<?php
session_start();
if(isset($SESSION['var'])){
echo "Yes it is";
}
else {
echo "No it isnt";
}
?>
Ass you can see, I put session_start(); in both pages but still nothing!
Any help would be much appriciated,
thank you!
P.S Im using XAMPP
To access session variables you need to access the $_SESSION. Change $SESSION to $_SESSION. Hope this helps.

session_start() not working in xampp

I created two files
1.php
2.php
which are in the same folder(i am using xampp).
In 1.php used session_start() and also used $_session['name']=abc. Then i opened 2.php to check whether session was created or not
2.php:
<?php
if(isset($_session['name'])){
echo $_session['name'];
}
else{
echo "no session found!!";
}
?>
and it keeps on saying "no session found!!"
Plz help ...
I searched a few sites n they say that by default d session is for whole folder containing
d script and session_set_cookie_params($lifetime,'/') (where $lifetime=60*60) is also nt helping.
On d other hand if at d end of1.php i use require("2.php") then abc is displayed.
What you have done is right in 1.php,
however, 2.php must start the session before using it.
2.php
<?php
session_start();
if(isset($_SESSION['name'])) {
echo $_SESSION['name'];
}
else{
echo "no session found!!";
}
?>
You're missing session_start() at the top of your 2.php file which is needed to access $_SESSION variables.
<?php
session_start(); // missing
if(isset($_SESSION['name']))
{
echo $_SESSION['name'];
}
else
{
echo "no session found!!";
}
?>
You need to call session_start(); again at the top of every page where you want to access $_SESSION variables, not only on the page where you want to initiate the session.
<?php
session_start();
if(isset($_SESSION['name'])){
echo $_SESSION['name'];
}else{
echo "no session found!!";
}
?>

Page Does Not Recognize SESSION

I have come along something i could not solve for so long.
i have created a script in php that unsets one single session variable, However the page stats the session Here is my code for the page :
<?php
session_start();
require_once("../header.php");
if($_SESSION['user']) {
unset($_SESSION['user']);
echo "you succesfully logged out.";
header("Refresh:5; url=http://www.webmasteroutlet.com");
} else {
echo "you are already NOT LOGGED IN right now.";
}
require_once("../footer.php");
?>
That is the whole code on this page. and it always prints out "you are already NOT LOGGED IN right now." $_SESSION['user'] is assigned true in login.php page and i have session_start(); at the very beginning of the page right after the <?php opening.
The session variable is recognized at all other files with php extension and that is the only single file that it is not working on. I also tried
<?php
session_start();
echo $_SESSION['user'];
?>
and it does not print anything. It simply skips that line and does nothing. What am i doing wrong ?
Thank You very much for your help.
this is the header.php code
<?php
session_start();
require("config.php"); // that only contains connection to the database and it is successful.
if(isset($_SESSION['user'])==1){
echo "<div id=\"topnav\" class=\"topnav\"><span>".$_SESSION['username']."</span> <span>LOGOUT</span></div>";
}
else if ($_SESSION['admin']) {
echo "<div id=\"topnav\" class=\"topnav\">"."<span>".$_SESSION['adminusername']."</span> ";
echo "<span>LOGOUT</span></div>";
}
else if ( !isset($_SESSION['user'])) {
require ($_SERVER['DOCUMENT_ROOT']."/users/login.php");
}
require("search.php");
?>
i think you need the if is set and make sure you pass the sessions data to this page it looks like your unsetting this
Try this:
<?php
session_start();
require_once("../header.php");
if(isset($_SESSION['user'])) {
echo "User.".$_SESSION['user']." you are being logged out";
unset($_SESSION['user']);
header("Refresh:5; url=http://www.webmasteroutlet.com");
} else {
echo "You are not logged or var SESSION doesnt exist";
}
require_once("../footer.php");
?>
If still doesnt work, try deleting the require_once lines(for debug).
Justin, I think you're not setting the $_SESSION['user']. That'd be the reason why you're getting NULL when you vardump.
One other possibility, although I'm limited to the scripts you provided, would be that you made it possible for a person to login through $_SESSION['admin'] as well as $_SESSION['user']. If this is the case you'd have to change the script to:
if(isset($_SESSION['user'])) {
unset($_SESSION['user']);
echo "user succesfully logged out.";
}elseif(isset($_SESSION['admin'])){
unset($_SESSION['admin']);
echo "admin succesfully logged out.";
}else{
echo "you are already NOT LOGGED IN right now.";
}

Help me with Php session vs Header redirect?

I have the following pages:
page1.php
<?php
if (isset($_GET['link'])) {
session_start();
$_session['myvariable'] = 'Hello World';
header('Location: http://' . $_SERVER['SERVER_NAME'] . dirname($_SERVER['REQUEST_URI']) . '/page2.php');
exit;
}
?>
Click Here
page2.php
<?php
print 'Here is page two, and my session variable: ';
session_start();
print $_session['myvariable']; //This line could not output.
exit;
?>
When I try output $_session['myvariable'] I did not get the result hello world message.
I could not find out the solution to fix it?
$_SESSION not $_session. Uppercase.
error_reporting(E_ALL); at the top of the script always helps in such case
session_start() has to be called before you send any output as it relies upon cookies to store the ID.
Also $_SESSION is uppercase
<?php
session_start();
echo $_SESSION['myvariable'];
echo 'Here is page two, and my session variable: ';
exit;
?>
HTTP headers must be the very first output, so session_start() must be at the top of your code.
Other notes:
* $_SESSION should be uppercase.
* Echo > print

Categories