php variables across files
We know how Variables and Cookies are important to take information from one page to another but it also involves so many drawbacks. Like, cookie can't store the specific variable information on the user's computer or variables can't take information to every new page. For this reason we use PHP session variable. PHP session variable is a method to store information and change the setting about the information provided by the person. PHP SESSION variable is to be used to take your information or address to multiple pages.
Let see the example where we took three different pages to take user information to every new page. The first page is a login.php page where the user fill his information and go the next page. The next page is home.php page where we will do php coding through which we send user information one page to another page and the last page will be the index.php page where we show the information of the person.
First Page (login.php)
echo "Please enter the valid username & password !";
<form action="home.php" method="post"><table width="300" align="center" cellpadding="4" cellspacing="5" border="2"><tr>
<td align="center">Username :</td><td align="center"><input type="text" name="uname"></input></td>
<td align="center">Age :</td> <td align="center"><input type="text" name="age"></input></td> </tr>
<td align="center">Password</td> <td align="center"><input type="text" name="pass"></input></td> </tr>
<td colspan=2 align="center"><input type="submit" name="submit" value="submitme"></input></td> </tr> </table>
In this page user login and fill his information. Then his information is send to the other page through post method and also we action to other page.
SECOND PAGE (home.php)
if($_POST["uname"]=="admin" || $_POST["pass"]=="admin")
$array["uname"] = "admin";
$array["age"] = "18";
$array["pass"] = "admin";
$_SESSION["user"] = $array;
In this page through session variable we store the user's information and then to the next page.
THIRD PAGE (index.php)
$abc = $_SESSION["user"];
foreach($abc as $key => $value)
echo $key . " " . $value . "<br/>";
Here, we display the information of the user. But one important think we give you just a example and you don' t display username or password information on the page. You can display other information about the users but not their password and username.
Ask your questions, our development team will try to give answers to your questions.