php check session data against URL parameter - php

I am new to php and having trouble with the following. I want to check the session user name to see if it matches the url parameter then print some stuff:
<?php
// Check User
if (isset($_SESSION['user_name'] == $_GET['name'])) {
//print html
}
?>
i just get a blank page when i test this even when the session name matches the url name.

You forgot to start your session and you're using isset() incorrectly
<?php
session_start();
// Check User
if ($_SESSION['user_name'] == $_GET['name']) {
//print html
}
?>
Better and more complete solution:
<?php
session_start();
// Check User
if (isset($_SESSION['user_name'])
&& isset($_GET['name'])
&& $_SESSION['user_name'] == $_GET['name']) {
//print html
}
?>

It seems you compare boolean result of isset function with string name from url.
// Check User
if (isset($_SESSION['user_name'])&&isset($_GET['name']))
if (($_SESSION['user_name'])==($_GET['name']))
{
//print html
}
?>

start session before checking session like this
<?php
session_start();
// Check User
if (isset($_SESSION['user_name']) == $_GET['name']) {
//print html
}
?>

logic error in isset() function, do so:
if (isset($_SESSION['user_name']) && isset($_GET['name']) && $_SESSION['user_name'] == $_GET['name']) {
//print html
}
upd: and sure, start_session() before any actions with session variables

Related

How to check session on page in php?

I have created the session on the login page and stored in a variable.
on the Login page
$_SESSION['user_id'] = $id;
Now I want to check session that exists or not?
On other pages
<?php
if (session_status() == PHP_SESSION_ACTIVE) {
echo 'Session is active';
}
You need to call
session_start();
on top of file to start the session.
To set a value,
$_SESSION['user_id'] = 1;
To check if session exists and not empty
if(!empty($_SESSION))
{
// write code here
}
To check for a particular value is set
if(isset($_SESSION['user_id']))
{
// write code here
}
You can use isset
if (isset($_SESSION['user_id'])) {
echo 'Session is active';
}

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>";
}
}

How to pass two value from one page to another in php, passing using session

How to pass two value from one page to another in PHP using session.
$account=$_SESSION["account_no"];
$account1=$_SESSION["account_no"];
Session will be available through out the application (in all pages) until you destroy it.
To set a session,
<?php
session_start();
$_SESSION['variable_name_1'] = "value_1"; // or $_POST['accountno_1'];
$_SESSION['variable_name_2'] = "value_2"; // or $_POST['accountno_2'];
?>
In the other page, to get the values
<?php
session_start();
echo $_SESSION['variable_name_1'];
echo $_SESSION['variable_name_2'];
?>
FILE-1: WHERE YOU NEED TO SAVE THE ACCOUNT TO SESSION
<?php // NOTICE THAT THERE IS NO SPACE BEFORE <?php [THIS IS IMPORTANT!!!]
// FILE-NAME: file_1.php WHERE YOU HAVE TO SET THE SESSION VARIABLE
//FIRST CHECK IF SESSION EXIST BEFORE STARTING IT:
if (session_status() == PHP_SESSION_NONE || session_id() == '') {
session_start();
}
$_SESSION["account_no"] = $account;
FILE-2: WHERE YOU NEED TO GET THE ACCOUNT FROM SESSION
<?php // NOTICE THAT THERE IS NO SPACE BEFORE <?php [THIS IS IMPORTANT!!!]
// FILE-NAME: file_2.php WHERE YOU NEED TO READ THE SESSION VARIABLE
//FIRST CHECK IF SESSION EXIST BEFORE STARTING IT:
if (session_status() == PHP_SESSION_NONE || session_id() == '') {
session_start();
}
// READ THE ACCOUNT NUMBER FROM SESSION DATA...
$account = $_SESSION["account_no"];
On the first page:
session_start();
$_SESSION['value1'] = 'First value';
$_SESSION['value2'] = 'Second value';
On the second page:
session_start();
$value1 = $_SESSION['value1'];
$value2 = $_SESSION['value2'];
File:1 where data will be store Session
<?php
session_start(); //before HTML tag
$_SESSION['one'] = $account_no1;
$_SESSION['two'] = $account_no2;
?>
File2: where you like to retrieve session
<?php
session_start();
echo $_SESSION['one'];
echo $_SESSION['two'];
?>
Sessions is a global variable in PHP.
Just create two session variables as use anywhere
<?php
session_start(); // should be at top of page or before any output to browser
$_SESSION['account'] = $account;
$_SESSION['account1'] = $account1;
Now access these session variables anywhere in any page but should start session before use, like:
<?php
session_start();
echo $_SESSION['account'];

Global Sessions?

I'm trying to get the variable from one page on my site to another using sessions but failing.
Example from page 1:
session_start();
$_session['error'] = "1";
Example from page 2:
session_start();
if ($_session['error'] == "1") {
print '<font color="#ff0000">You need to sign in with a username!</font>';
}
$_SESSION and $_session are two different variable one is basic variable and another is GLOBAL variable.
You need to the GLOBAL one. As you are using the variable in two different page so you have to go with the uppercase one that is $_SESSION. If you store at that variable you can access the variable from any page in the same domain with the help of session_start.
Solution:
Page_1.php
session_start();
$_SESSION['error'] = "1";
page_2.php
session_start();
if ($_SESSION['error'] == "1") {
print '<font color="#ff0000">You need to sign in with a username!</font>';
}
You need to use $_SESSION['error'] instead of $_session['error']. $_SESSION stores information in the session whereas $_session is just a variable on the page because it's lowercase. Thus your pages become
Example from page 1:
session_start();
$_SESSION['error'] = "1";
Example from page 2:
session_start();
if ($_SESSION['error'] == "1") {
print '<font color="#ff0000">You need to sign in with a username!</font>';
}

condition to check if the session variables as well as get and post have been destroyed in php

The code is like:(It is the last page of the web-app I have made)
<?php
if(isset($_GET['var'])
{
session_start();
$a=$_SESSION['prev_defined'];
#more use of session variable
session_destroy();
$_SESSION = array();
unset($_GET);
unset($_POST);
}
?>
Now when i execute the web application it runs fine , when i refresh the last page whose code is given above the warning message shows of undefined symbol because the $_SESSION variables as well as $_GET and $_POST have been deleted. I want to display message "SESSION OVER" on refresh. How to do it? Where to put if condition? I have tried to put the above code in
if($_SESSION)
{
#entire code above
}
else
{
echo"SESSION OVER";
}
but it displayes message undefined variable _SESSION
<?php
if(isset($_GET['var'])
{
if(isset($_SESSION))
{
session_start();
$a=$_SESSION['prev_defined'];
#more use of session variable
session_destroy();
$_SESSION = array();
unset($_GET);
unset($_POST);
}
else
{
echo"SESSION OVER";
}
}
?>
Try this one. If session is set it will do the conditions.
EDIT
if(isset($_GET['var'])
{
....
}
else
{
echo"SESSION OVER";
}
Using this, If $_GET['var'] is not set then echo the else part
EDIT 2
<?php
if (isset($_GET) || isset($_SESSION)) {
//Put all your codes here
} else {
echo "Session Over";
}
$_SESSION is a global variable, an array. And it will be allways around.
check if session feature is active like so:
if (session_status() == PHP_SESSION_NONE) {
session_destroy();
echo "session over";
}
Also note to check if arrays key isset, before checking it's value, to avoid notices:
if(isset($_SESSION['login'])&&$_SESSION['login']==1){//pass}
EDIT:
as stated here: For versions of PHP < 5.4.0
if(session_id() == '') {
session_start();
}

Categories