Saturday 25 June, 2011

Lesson 15 : PHP Query String.

Retweet this button on every post blogger
Query string is usual part of any website it sits in the URL bar and associated with  website URL for navigation to the different sections of the website. use of query string to send data of one page to others.
Example of query string;
index.php?page=user&role=admin


The query string always starts with Question mark (?) and separates two variables with address operator(&).

Example program  of Query string  :-
page page1.php
<?php
if ( isset ( $_POST['submit'] ) )
{
     $url = "nextpage.php?fname=".$_POST['fname']."&lname=".$_POST['lname']."&fdigit=".$_POST['fdigit']."&sdigit=".$_POST['sdigit'];
header("Location:$url");
}


?>


<html>
<head><title>PHP Query String.</title></head>
<body>
<center>
<form action="<?php $_SERVER['PHP_SELF'];?>" method="post">
<table cellpadding="0" cellspacing="0">
<tr><td>Firt name</td><td><input type="text" name="fname" /></td></tr>
<tr><td>Last name</td><td><input type="text" name="lname" /></td></tr>
<tr><td>First digit</td><td><input type="text" name="fdigit" /></td></tr>
<tr><td>Second digit</td><td><input type="text" name="sdigit" /></td></tr>
<tr><td>&nbsp;</td><td><input type="submit" name="submit" value="Go.." /></td></tr>
</table>
</form>
</center>
</body>
</html>


This program contains nothing the whole magic at the nextpage.php  


Page nextpage.php


<html>
<head><title>Next page</title></head>
<body>
<center>
<table cellpadding="0" cellspacing="0">
<tr><td>Firt name</td><td><?php echo $_GET['fname'];?></tr>
<tr><td>Last name</td><td><?php echo $_GET['lname'];?></td></tr>
<tr><td>First digit</td><td><?php echo $_GET['fdigit'];?></td></tr>
<tr><td>Last digit</td><td><?php echo $_GET['sdigit'];?></td></tr>
<?php

$sum  = $_GET['fdigit']+$_GET['sdigit'];
?>
<tr><td>Sum:</td><td><?php if ( isset ( $sum ) ) echo $sum; ?></td></tr>

</table>
</center>

</body>
</html>
There are two option to get value from a query string $_GET and $_REQUEST these both are PHP globals array.
The major drawback with Query string  that they can be manipulate while running.
TIP :- NEVER SEND SENSITIVE DATA USING QUERY STRING BECAUSE ITS SHOWS WHAT IS CONTAINS.   

6 comments: