Home Tutorial Php PHP Variables Across Files

 
 

Share on Google+Share on Google+
PHP Variables Across Files
Posted on: January 9, 2010 at 12:00 AM
Advertisement
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>

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

</tr>

</table>

</form>

</body>

</html>

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

session start();

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

{

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

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

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

$_SESSION["user"] = $array;

header("Location:index.php");

}

else

{

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

}

?>

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

 

THIRD PAGE (index.php)

<?php

session_start();

$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.

Advertisement

Related Tags for PHP Variables Across Files:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: January 9, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com