October 27, 2010

passing variables from php file to anther

Question by OdO

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;

?>

Answer by kijin

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'

Answer by Starx

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.

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!