PHP Variables Across Files


 

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.

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 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)

<html>

<head>

<title>Login.php</title>

</head>

<body>

<?php

if(isset($_GET["error"]))

{

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>

</tr>

<tr>

<td align="center">Age :</td>

<td align="center"><input type="text" name="age"></input></td>

</tr>

<tr>

<td align="center">Password</td>

<td align="center"><input type="text" name="pass"></input></td>

</tr>

<tr>

0

<td colspan=2 align="center"><input type="submit" name="submit" value="submitme"></input></td>

</tr>

</table>

1

</form>

</body>

</html>

2

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)

<?php

3

session start();

if($_POST["uname"]=="admin" || $_POST["pass"]=="admin")

{

4

$array["uname"] = "admin";

$array["age"] = "18";

$array["pass"] = "admin";

5

$_SESSION["user"] = $array;

header("Location:index.php");

}

6

else

{

header("Location:login.php?error=1");

7

}

?>

In this page through session variable we store the user's information and then to the next page.

8

 

THIRD PAGE (index.php)

<?php

9

session_start();

$abc = $_SESSION["user"];

foreach($abc as $key => $value)

0

{

echo $key . " " . $value . "<br/>";

}

1

?>

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.

Ads