I am currently in file1.php and I want a variable from file1.php to pass to file2.php
How do I pass a variable from one php file to another?
I too want to go from file1.php to file2.php
I did following in file1.php:
session_start();
$_SESSION['akknum']="jkl";
header("Location: file2.php");
and did following in file2.php :
<?php
echo "abc";
echo $_SESSION['akknum'];
?>
The file2.php is opening and abc is printing but no value of "jkl" is printing. What am I doing wrong?
You need to add session_start(); in file2.php.
session_start();
echo "abc";
echo $_SESSION['akknum'];
Basic usage.
you need to session_start the second file too.
If you want to use $_SESSION on a different file, you'll need to call session_start();
file1.php
<?php
session_start();
$_SESSION['akknum']="jkl";
header("Location: file2.php");
?>
file2.php
<?php
session_start();
echo $_SESSION['akknum'];
//jkl
?>
Read more about php sessions
Related
I have been stuck to get session data on second page, following are sample code for both pages, but on second page I can't get any value in session
for a.php
<?php
session_start();
$_SESSION["captchacode"] = 123456;
?>
for b.php
<?php
session_start();
echo "<pre>";
print_r($_SESSION);
var_dump($_SESSION);
die;
?>
So I have a following code snippet:
first page:
<?php
session_start();
var_dump($_SESSION);
function dispatcher($url){
include "$url.php";
}
dispatcher($_GET['url']);
second page:
$_SESSION['foo'] = 'bar';
The problems is, when I go to secondpage.php and come back to the first page, the $_SESSION array is empty. Any ideas?
I have a simple question, with maybe not so simple of an answer. I want to be able to set a variable in one script and pass that value and variable to another script. Without passing it through the url, and having the ability to pass an array.
So I have index.php in there I have a variable
<?php
$myvariable = '';
<form action=editRecord.php>
do some form actions here and submit moving to editRecord.php
</form>
?>
Now in editRecord.php
<?php
header('Location: go back to index.php);
run functions and edit the Mysql DB
//Ok now here is where I want to assign a value to $myvariable and pass it back to index.php
?>
Is this possible? I am sorry for the amateurish question, but I am very green when it comes to php.
Thank You.
you can set it in the $_SESSION variable:
<?php
session_start();
$_SESSION["myVar"] = "blabla";
....
Of course you can store an array() in this variable too.
Just pass that information in the querystring. Then you can access it via $_GET. If it doesn't exist, just set the value to an empty string or whatever default value you want it to have.
// editRecord.php
<?php
// do db dtuff
header('Location: index.php?myvariable=somevalue');
?>
// index.php
<?php
$myvariable = (isset($_GET['myvariable'])) ? $_GET['myvariable'] : '';
<form action="editRecord.php" >
</form>
?>
You can also use session variables:
// editRecord.php
<?php
session_start();
// do db stuff
$_SESSION['myvariable'] = 'somevalue';
header('Location: index.php');
?>
// index.php
<?php
session_start();
$myvariable = (isset($_SESSION['myvariable'])) ? $_SESSION['myvariable'] : '';
<form action="editRecord.php" >
</form>
?>
You can use sessions, POST data, GET data, cookies and database data.
page1.php
$_SESSION['sVar'] = 'XYZ';
header('Location:page1.php');
page1.php
echo $_SESSION['sVar']; // Works well
page1.php
echo $_SESSION['sVar']; // donot get displayed after adding following line
unset($_SESSION['sVar']);
My Question is : When I am echo session variable it works well but when i destroy that variable after echo its not getting echo.
This works fine for me
session1.php :
<?php
session_start();
$_SESSION['sVar'] = 'somecrap';
header("Location: session2.php");
exit;
session2.php :
<?php
session_start();
echo $_SESSION['sVar'];
unset($_SESSION['sVar']);
Obviously if I refresh session2.php I get an undefined index warning
Are you starting the session in both pages?
How to pass variables from a php file to another while it is not html inputs ,just i have a link refer to the other file and i want to pass variables or values to it
Example:
File1.php
<?php
$name='OdO';
echo "<a href='File2.php'>Go To File2</a>";
?>
File2.php
<?php
echo $name;
?>
Use sessions to store any small value that needs to persist over several requests.
File1.php:
session_start();
$_SESSION['var'] = 'foo';
File2.php:
session_start();
$var = $_SESSION['var']; // $var becomes 'foo'
Try to use sessions. Or you can send a GET parameters.
You can use URLs to pass the value too.
like
index.php?id=1&value=certain
and access it later like
$id = $_GET['id'];
$value = $_GET['value'];
However, POST might be much reliable. Sessions/Cookies and database might be used to make the values globally available.
Here's one (bad) solution, using output buffering:
File 1:
<?php
$name = 'OdO';
echo 'Go To File2';
?>
File 2:
<?php
ob_start();
include 'File1.php';
ob_end_clean();
echo $name;
?>