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>';
}
Related
I want to execute some PHP code by clicking on a link and Passing variables to another page with url
Pleas help me.
Such WordPress Site: https://www.armandl.com/?p=5123
You set $_SESSION['status'] to 0 and then set it to 1, so it will always be 1. Additionally, the link you click has nothing to do with the session vars.
Page 1:
Normally to get just one variable from one page to another via hyperlink you would add it to the URL as a query parameter and access it using $_GET:
<?php
echo 'No';
echo 'Yes';
?>
Page 2:
You also need to check if $_GET['status'] is set:
<?php
if (isset($_GET['status']) && $_GET['status'] == 0) {
echo "No !";
}
elseif (isset($_GET['status']) && $_GET['status'] == 1) {
echo "Yes !";
}
else {
echo "NOT set !";
}
?>
Now, if you want it to be accessible on other pages without passing in the URL, then set it as a session var:
session_start();
$_SESSION['status'] = $_GET['status'];
You can also use any type of variable & string instead of the 0,1 selection.
Example:
if (isset($GET["newpwd"])){
if ($_GET["newpwd"] == "passwordupdated"){
//code goes here
}
}
To pass from another page you would use something like:
header("Location: ../signup.php?newpwd=passwordupdated");
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 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'];
Hi i'm developing a multi steps form with php using session and i've been wondering if there is a way for the user to alter session variables for example on the first page i have something like this :
<?php
session_start();
if(isset($_POST['submit'])){
$_SESSION['name'] = $_POST['name'];//and so on
}
?>
and the other page has something like :
<?php
session_start();
$name = $_SESSION['name'];
?>
my question is can the user modify the value of the session variable on the second page
Since you're populating the session variable with the value of a POST variable, they can continue to resubmit the first form as much as they want with arbitrary values.
You can use application logic to defeat this:
<?php // form1
session_start();
if (empty($_SESSION['step'])) {
$_SESSION['step'] = 1;
}
if ($_SESSION['step'] > 1) {
header("Location: form2.php");
exit; // This exit is very important, don't neglect it
}
if (isset($_POST['submit'])){
$_SESSION['name'] = $_POST['name'];//and so on
$_SESSION['step'] = 2;
}
And then
<?php // form2
session_start();
if (empty($_SESSION['step'])) {
header("Location: form1.php");
exit;
}
if ($_SESSION['step'] > 2) {
header("Location: form3.php");
exit;
}
if ($_SESSION['step'] < 2) {
header("Location: form1.php");
exit;
}
$name = $_POST['name'];
By using application logic, you can control the flow of your visitors within your application.
If you're asking if users can change $_SESSION variables outside of any code you've written, the answer is usually no. See also: this answer.
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();
}