Introduction
There are basically four ways to do this: pass the variable in the URL, through a session, via a cookie, or with an HTML form. The method you choose depends on the situation and what best fits your needs at the time, we are basically discussing in this article passing variables through a URL.
The first method of passing variables among pages is through the page's URL. You have undoubtedly seen URLs such as this:
http://localhost/get.php?id=123
This an example of passing variable values through the URL. It requests that the article with the ID number of "123" be chosen for the "get.php" program. The text after the URL is called the query string. You can also combine variables in URLs by using an ampersand (&), as in this example:
http://localhost/get.php?id=123&language=english
This asks to retrieve the file with an ID of "123" and the language equal to "english". There are a few disadvantages to passing variables though a URL; they are:
- Everyone can see the values of the variable, so passing sensitive information is not really very secure using this method.
- The user can arbitrarily change the variable value in the URL and try deferent combinations, leaving your web site potentially open to showing something you did not intend to be shown.
- A user might also pull up inaccurate or old information using a saved URL with older variables embedded in it (from a bookmark, for example).
Variables that you pass around in this way are accessible in your PHP code through the special $_GET array. The variable name that appears in the URL is used as a key, so to retrieve the value of "id" you would reference $_GET['id'], or to retrieve the value of language you would reference $_GET['language'].
Example
<html>
<head>
<title>my compny info-<?php echo $_GET['name'];?></title>
</head>
<body>
<?php
echo 'My company name is ';
echo $_GET['name'];
echo '<br>';
$No = 8506800799;
echo 'Contact no is ';
echo $No;
?>
</body>
</html>
Save this file a "get.php".
<html>
<head>
<title>Find my company</title>
</head>
<body>
<?php
echo '<a href="get.php?name=MCN Solution">';
echo 'Click here to see information about my compony!';
echo '</a>';
?>
</body>
</html>
Save this file as "get2.php" and open it in your browser. Your screen should look as in the following.
Output
Special character in URLs
Passing a variable through a URL poses an interesting problem if there are spaces, ampersands, or other special characters in the value of your variable. There is a special function called "urlencode()" to be used when passing these values through a URL. If you wanted to change your name from "MCN Solution" to "Microsoft" you would use urlencode() to encode the value and insert the proper HTML special characters.
Make the following highlighted changes to your "get2.php" file:
<html>
<head>
<title>Find my compony</title>
</head>
<body>
<?php
$company = urlencode('My MCN Solution');
echo "<a href=\"get.php?name=$company\">";
echo 'Click here to see information about my compony!';
echo '</a>';
?>
</body>
</html>
Output