how to pass session value other page if condition true in php - 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>";
}
}

Related

Passing variables to another page with url - PHP

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

How to redirect a page with variable value if condition is true in php?

how to jump a page if condition true and also i want to sent a variable value to secont page
if(isset($_POST['compair']))
{
$_SESSION['usermail'];
$answer=$_POST['answer'];
if ($answer == $_SESSION['answer'])
{
$mail=$_SESSION['usermail'];(i want to sent "$mail" variable)
header("Location:resetpass.php?value = $mail ");
}
else
{
echo "<script>alert('Please Try again')</script>";
}
}
please also tell me how to receive this variable on second page.
Your solution is correct. Just pay attention to spaces:
header("Location: resetpass.php?value=$mail");
exit; // as suggested by "nogad"
Also make sure that resetpass.php file is in the same directory of current page.
In resetpass.php you can get the variable by $_GET['value'] like:
<?php
if( isset($_GET['value']) ){
$mail = $_GET['value'];
}
After header("Location : resetpass.php?value=$mail");
exit(); // i.e quit the current page and go to resetpass.php
Then at resetpass.php collect $mail using the GET method.
if(isset[$_GET['value'])){
$the_mail = $_GET['value'];
}
I am writing from my handy. So apologies for any layout discripancies

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

Need help making an if statement in php that creates a html href link when passed correctly

<?php if ($_SESSION["isLoggedOn"] == True) {?>
LOGOUT
<?php }?>
i want the link to only be created if the IF statement is successful and to not create it if it is not. At the moment no matter what i have tried to change the link will always be created. thanks.
Have you tried
<?php
if ($_SESSION["isLoggedOn"] == True)
{
echo 'LOGOUT';
}
?>
Your this visible code seems to be corrected. May be you would have some error in storing the sessions. Try:
echo $_SESSION['isLoggedOn'];
OR try doing this:
<?php
if ($_SESSION['isLoggedOn']) {
echo "Logout";
}?>
<?php if (isset($_SESSION["isLoggedOn"]) && $_SESSION["isLoggedOn"] == True) {?>
LOGOUT
<?php }?>
Your $_SESSION["isLoggedOn"] is always TRUE.
Also dont use == TRUE for True/False variables.
Also logging out at terms and conditions page is not very good i guess ;-)
Elaborate your code:
<?php
session_start();
if ( !isset($_SESSION) || !isset($_SESSION["isLoggedOn"]) {
echo "an error occured instantiating the session.";
exit 1;
}
if ( $_SESSION["isLoggedOn"] === true ) {
echo 'LOGOUT';
}
?>
You need to start a session as very first action in your PHP.
Check if you actually have a vale in $_SESSION
Then, check its state and output what you want.

Remove item from session (show a remove button)

I have a button on a page, It's a simple input submit button, which sends the page ID to a form on another page to save it in a PHP session.
I was wondering how I'd go about showing a remove button instead if that item is already in your session?
This is the code that the form gets sent to
<?php
session_start();
if(!in_array($_POST['event_id'], $_SESSION['event_orders'])) {
$_SESSION['event_orders'][] = $_POST['event_id'];
}
?>
The page with the button doesn't actually have any session code, but I guess it would be a similar IF kind of statement, but I am unsure of the exact syntax I should use? And how would it take the ID away from session?
EDIT:
Would something like this work? To put around the form button.
<? if(isset($_SESSION['event_orders']))
{
echo "<a href='".$_SERVER['PHP_SELF']."?delete'>delete</a>";
}
else if(!in_array($_POST['event_id'], $_SESSION['event_orders'])) {
// echo the submit button;
} ?>
You can use unset function. Reference: http://php.net/manual/en/function.unset.php
<?php
session_start();
//if request parameter delete is present then unset the array
if(isset($_REQUEST['delete']))
{
unset($_SESSION['event_orders']);
}
else if(isset($_SESSION['event_orders']))
{
//displays delete link which redirects to same page with an additional request parameter as url?delete
echo "<a href='".$_SERVER['PHP_SELF']."?delete'>delete</a>";
}
else if(!in_array($_POST['event_id'], $_SESSION['event_orders'])) {
$_SESSION['event_orders'][] = $_POST['event_id'];
}
?>
EDIT:
we need only to delete the specific array items instead of fully unseting the array.
<?php
session_start();
//if request parameter delete is present then unset the array
if(isset($_REQUEST['delete']))
{
unset($_SESSION['event_orders'][$_REQUEST['delete']]);
var_dump($_SESSION['event_orders']);
}
else if(in_array($_POST['event_id'], $_SESSION['event_orders']))
{
//displays delete link which redirects to same page with an additional request parameter as url?delete
$key = array_search($_POST['event_id'], $_SESSION['event_orders']);
var_dump($_SESSION['event_orders']);
echo "<a href='".$_SERVER['PHP_SELF']."?delete=".$key."'>delete</a>";
}
else
{
$_SESSION['event_orders'][] = $_POST['event_id'];
var_dump($_SESSION['event_orders']);
}
?>

Categories