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");
Related
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 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
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>';
}
So I am creating an application (new -ish to sessions) and have been trying to create a simple error handling statement using sessions.
To simplify things, let me describe the basics.
User enters query on page 1
User presses submit
Page 2 is loaded to check the value of the query
If the query is set, it is run and the result is displayed on page 3
If it is empty however, an error is caught, set and the user is redirected back to page 1
What I care about is the last step as I cannot get it to work for some reason.
Here is the code pertaining to the error:
Page 1s relevant code:
<?php
session_start();
$ERROR = $_SESSION['error'];
if($ERROR) {
echo $ERROR;
}
?>
And on page 2:
<?
session_start();
---------------- And as we go down the file a bit ----------------
if(trim($getQuery == "")){
$ERROR = "no search criteria entered";
$_SESSION['error'] = $ERROR;
if(!isset($_SESSION['error'])) {
die("the session error was not set for some reason");
}
$url = "localhost:8000/mysite"; //index.php is page 1 in this case so I just redirect to the parent directory as index is loaded by default obviously in that case
header("Location:" . $url);
}
?>
$getQuery is the value captured in the query box on page 1 and sent via the post method to page 2 as you may assume naturally.
But when I enter nothing in the query box and then send the query, the page refreshes (as it should when page 2 realises that the query is empty and header location reloads the page) but no error is shown, which it should considering I check on page 2 that it is set.
Any ideas?
Cheers,
-- SD
You have a typo in if(trim($getQuery == "")) { ... } it should be if(trim($getQuery) == "") { ... }, since you only want to trim the $getQuery variable, and not the whole condition. If you change this, then it will work.
Here's a minimum working example
<?php // 1.php
session_start();
$ERROR = $_SESSION['error'];
if($ERROR) {
echo $ERROR;
}
?>
<?php // 2.php
$getQuery = ""; // This is empty so it will redirect to 1 and show error message
session_start();
if(trim($getQuery) == ""){
$ERROR = "no search criteria entered";
$_SESSION['error'] = $ERROR;
if(!isset($_SESSION['error'])) {
die("the session error was not set for some reason");
}
$url = "1.php"; //index.php is page 1 in this case so I just redirect to the parent directory as index is loaded by default obviously in that case
header("Location:" . $url);
}
?>
Sometimes the browser redirect (your header("Location")) can be quicker than the server.
Just before the redirect you should put
session_write_close()
Just to make sure the session is written for next time.
I have a main page that users go to that shows output from a MySQL query depending on the variable passed to it. So;
http://website/mypage.php?page=0
However, I would like to set up redirection so that if someone just goes to
http://website/mypage.php
that it will go to http://website/mypage.php?page=0. I thought of using the following code, which verifies the current page as well as verifies that a user's session is established;
elseif ($_SERVER['PHP_SELF'] == '/mypage.php' && isset($_SESSION['valid_user']))
{
header('Refresh: 0; URL=/mypage.php?page=0');
}
But, this looks to be too general. Is there a way to check for exactly '/mypage.php' or maybe '/mypage.php?page=' ? I thought of using strlen to check for only the 11 characters in /mypage.php, but I'm not sure that this is the most efficient way of doing it.
You can check to see if the variable page has a value and if its empty you can do the redirect
if($_GET['page'] == ''){
header('Location: /mypage.php?page=0');
exit;
}
or
if(!isset($_GET['page'])){
header('Location: /mypage.php?page=0');
exit;
}
In your mypage.php you can check something like this
if(!isset($_GET['page'])) {
header('location: /mypage.php?page=0');
exit;
}
But, I think, instead of redirecting to same page with a get variable, why don't just show the page you want to show by default, when there is no page variable is set.
You should use this way:
header('Location: /mypage.php?page=0');
exit;
Otherwise check the $_SERVER variables for more strict match. http://php.net/manual/en/reserved.variables.server.php
I think probably you need $_SERVER['REQUEST_URI']
Is this what you're looking for?
if(!isset($_GET['page']) || empty($_GET['page'])) {
Header('Location: http://website/mypage.php?page=0')
exit();
}
or
if(!isset($_GET['page']))
$_GET['page'] = 0;
I'm not sure about the second solution, you shouldn't attribute value to $_GET[] variables.
if( !isset($_REQUEST['page']) ) {
header('Location: /mypage.php?page=0');
exit(0);
} else {
if( $_REQUEST['page']=="" ) {
header('Location: /mypage.php?page=0');
exit(0);
}
}