Need advice about transfering HTTP_REFERER to another page - php

So I'm currently trying to save page1.php's HTTP_REFFERER as a variable and transfer that variable to another page. I've tried using $_SESSION[] and $_COOKIES[] methods but it didn't work. page2.php shows referrer as page.php.
Any suggestions?
page1.php :
<?php session_start();
$variable1 = $_SERVER['HTTP_REFERER'];
$_SESSION['ref'] = $variable1;
page2.php :
<?php session_start();
$_SESSION['ref'] = $variable1;
echo $variable1; ?>

You inverted the variable and the value in your 2nd page
<?php
session_start();
$variable1 = $_SESSION['ref'];
echo $variable1;

$_SERVER['HTTP_REFERER'] is a predefined env variable. To access stored variable use for example $_SESSION['HTTP_REFERER'] on page2.php

In page1.php, make sure to end the code correctly:
<?php session_start();
$variable1 = $_SERVER['HTTP_REFERER'];
$_SESSION['ref'] = $variable1;
?>
In page2.php just print_r the $_SESSION['ref'] or assign it to a variable and then use echo.
Also the order of the variables is incorrect, should be:
$variable1 = $_SESSION['ref'];
HTH.

Related

Can't pass variable from one file to another using $GLOBALS array [duplicate]

I have three files - global.php, test.php, test1.php
Global.php
$filename;
$filename = "test";
test.php
$filename = "myfile.jpg";
echo $filename;
test1.php
echo $filename;
I can read this variable from both test and test1 files by
include 'global.php';
Now i want to set the value of $filename in test.php and the same value i want to read in test1.php.
I tried with session variables as well but due to two different files i am not able to capture the variable.
How to achieve this........
Thanks for help in advance.....
Use:
global.php
<?php
if(!session_id()) session_start();
$filename = "test";
if(!isset($_SESSION['filename'])) {
$_SESSION['filename'] = $filename;
}
?>
test.php
<?php
if(!session_id()) session_start();
//include("global.php");
$_SESSION['filename'] = "new value";
?>
test1.php
<?php
if(!session_id()) session_start();
$filename = $_SESSION['filename'];
echo $filename; //output new value
?>
I think the best way to understand this is as following:
You have one file index.php whit some variable defined.
$indexVar = 'sometext';
This variable is visible on all index.php file. But like a function, if you include other files, the variable will not be visible unless you specify that this variable has scope global.
You should redefine the scope of this variable in your new file, like this:
global $indexVar;
Then you will be able to acess directly by calling it as you were in your main file. You could use an "echo" in both files, index.php and any other file.
First you start session at the top of the page.
Assign your variable into your session.
Check this and Try it your self
test.php
<?php
session_start(); // session start
include("global.php");
$filename = "myfile.jpg";
$_SESSION['samplename']=$filename ; // Session Set
?>
test1.php
<?php
session_start(); // session start
$getvalue = $_SESSION['samplename']; // session get
echo $getvalue;
?>

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

Webmatrix session_destroy()

I have written a simple code for $_SESSION variable in the first php file:
<?php
session_start();
$_SESSION["name"] = "John";
?>
and in another php file to render this:
<?php
session_start();
echo $_SESSION["name"];
?>
But after that i used session_unset(); and session_destroy(); and after that i can't render any new $_SESSION variable nor the existing one. I am using Microsoft WebMatrix program and Chrome as main browser. Any suggestions? Thank you in advance.
That is because session_destroy(); destroys the current session and also sends a header to the browser to delete the session variable. In the same time the session is deleted on the server (in PHP) and the $_SESSION variables can no longer be used. You can always try to save the $_SESSION in another variable;
session_start();
$_SESSION['test'] = 'foo';
Next page:
session_start();
$saveSession = $_SESSION;
session_destroy();
var_dump($_SESSION); //Gives an empty array
var_dump($saveSession); //Still has ['test' => 'foo']
More information: http://php.net/manual/en/function.session-destroy.php and http://php.net/manual/en/book.session.php
Also, sidenote, you do not need to open and close PHP tags if they are combined;
<?php
session_start();
echo $_SESSION["name"];
?>
works just as well as
<?php
session_start();
?>
<?php
echo $_SESSION["name"];
?>

php session can not pass variable to other page

I have a formal code, but the session variable can't be passed to another page.
So here is my sample code. However, the result of this code is 0.:
page1 code:
<?php
session_start();
$_SESSION['try'] = 5;
header('Location: page2.php');
?>
page2 code:
<?php
session_start();
$test = $_SESSION['try'];
echo $test;
?>
From the manual:
session_start() creates a session or resumes the current
one based on a session identifier passed via a GET or POST request,
or passed via a cookie.
pay attention to the last two lines of code:
<?php
// page1.php
session_start();
echo 'Welcome to page #1';
$_SESSION['favcolor'] = 'green';
$_SESSION['animal'] = 'cat';
$_SESSION['time'] = time();
// Works if session cookie was accepted
echo '<br />page 2';
// Or maybe pass along the session id, if needed
echo '<br />page 2';
?>

PHP Sessions variables

I have 3 pages, one HTML page (session.html) and 2 PHP pages (session.php & sessiona.php). The html page has a form on it with only 2 fields, one being a text box (customername) and other being a selection list (hobby). The action on the HTML page is to send to session.php in which i store the variables in this manner :
<body>
<?php
session_start();
$_session['name'] = $_POST['customername'];
$_session['hobby'] = $_POST['hobby'];
$name = $_session['name'];
$hobby = $_session['hobby'];
?>
<p>Sessiona</p>
</body>
The point of session.php is only to store the variables and only display a link to sessiona.php which i want to retrieve the stored variables from sesssion.php and diplay them. Is there a way to do this?
It should suffice to set
session_start();
in your sessiona.php where you can get the variables in the manner you suggested:
$name = $_SESSION['name'];
$hobby = $_SESSION['hobby'];
Any page to start the session variable's creation
<?php session_start();
$_SESSION['name'] = $_POST['customername'];
$_SESSION['hobby'] = $_POST['hobby'];
?>
<html>...</html>
Any page to retrieve that session data after it's created
<?php
if(isset($_SESSION['name'])) { $name = $_SESSION['name'] }
if(isset($_SESSION['hobby'])) { $hobby = $_SESSION['hobby'] }
?>
<html>...<?php echo($name) ?>...or...<?php echo($_SESSION['name'])?>...</html>
Note: you will want to check for that session variable to ensure you don't throw an error on echo. isset(), !empty(), etc.

Categories