UNIT – III Chapter 5 - State Management - Copy
UNIT – III Chapter 5 - State Management - Copy
Here’s a simple example that creates two variables, $firstName and $age, then creates a link in the displayed page
that contains a query string to store the variable values:
<html>
<body>
<?php
$firstName = "John";
$age = "34";
$queryString = "firstName=$firstName&age=$age";
echo '<p><a href="moreinfo.php?' . $queryString . '" > Find out more info on this person </a> </p >';
?>
</body>
</html>
This code generates the following markup:
< p > < a href= “moreinfo.php?firstName=John&age=34”> Find out more info on this person < /a > < /p >
If the user then clicks this link, moreinfo.php is run, and the query string data ( firstName=John & age=34 ) is
passed to the moreinfo.php script. Data has been transmitted from one script execution to the next.
The ampersand ( & ) character needs to be encoded as & amp; inside XHTML markup.
One thing to watch out for is the type of characters that you insert into the field names and values in your query
string.
The specifications for a query string allows only the following characters to be used within field names and values:
letters, numbers, and the symbols - , , . (period), ! , ~ , * , ‘ (single quote), ( , and ) .
So what do you do if you need to transmit other characters, such as spaces, curly braces, or? characters? The answer
is that you should use URL encoding. This is a scheme that encodes any reserved characters as hexadecimal
numbers preceded by a percent ( % ) symbol, with the exception of space characters, which are encoded as plus ( + )
signs.
PHP gives you a function called urlencode() that can encode any string using URL encoding. Simply pass it a string
to encode, and it returns the encoded string. So you can use urlencode() to encode any data that may contain
reserved characters.
Here’s an example:
<html>
<body>
<?php
$firstName = "John";
$homePage = "http://www.shahucollege.com/";
$favoriteSport = "Ice Hockey";
$queryString = "firstName=" . urlencode($firstName) . " & amp;homePage=" .urlencode( $homePage ) . "
&favoriteSport=" . urlencode( $favoriteSport );
echo '<p><a href="moreinfo.php?'.$queryString .'"> Click to more information </a></p>';
?>
</body>
</html>
This code snippet outputs the following markup:
<p><a href=”moreinfo.php?firstName=John%20&%20amp;homePage=http%3A%2F%2Fwww.shahucollege.com%2F%20
&favoriteSport=Ice+Hockey” ” > Find out more info on this person < /a > < /p >
htmlform.html
<html>
<body>
<form action="dispinfo.php" method="post">
<label> Enter Student Name:</label>
<input type="text" name="sname"></br>
<label> Enter Roll No:</label>
<input type="text" name="srno"></br>
<label> Enter Class:</label>
<input type="text" name="sclass"></br>
<label> Enter Semester:</label>
<input type="text" name="sem"></br>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
Displaying Information and Setting Cookie:
dispinfo.php
<?php
$sname=$_POST["sname"];
$srno=$_POST["srno"];
$sclass=$_POST["sclass"];
$sem=$_POST["sem"];
echo "Student Name:".$sname."</br>";
echo "Student Roll No:".$srno."</br>";
echo "Student Class:".$sclass."</br>";
echo "Student Semester:".$sem."</br>";
setcookie("studcookie",$_POST["sname"]." ".$_POST["srno"]." ".$_POST["sclass"]."
".$_POST["sem"],time()+3600);
echo "cookies set Successfully! ";
?>
To access the session data we set on our previous example from any other page on the same web domain — simply
recreate the session by calling session_start() and then pass the corresponding key to the $_SESSION associative
array.
<?php
session_start();
// Accessing session data
echo 'Hi, ' . $_SESSION["firstname"] . ' ' . $_SESSION["lastname"];
?>
However, to destroy a session completely, simply call the session_destroy() function. This function does not need
any argument and a single call destroys all the session data.
<?php
session_start();
// Destroying session
session_destroy();
?>
Every PHP session has a timeout value. Timeout value is a duration, measured in seconds, which determines how
long a session should remain alive in the absence of any user activity.
Example:
<?php
session_start();
if(isset($_SESSION["count"])) {
$accesses = $_SESSION["count"] + 1;
} else {
$accesses = 1;
}
$_SESSION["count"] = $accesses;
// session_destroy();
?>
<html>
<head>
<title>Access counter</title>
</head>
<body>
<h1>Access counter</h1>
<p>You have accessed this page <?php echo $accesses; ?> times today.</p>
<p>[<a href="session.php">Reload</a>]</p>
</body>
</html>
==0==