PHP Variables from URL


 

PHP Variables from URL

Are you facing problem passing variable from URL? Don't worry, it is quite easy to learn and implement into your project.

Are you facing problem passing variable from URL? Don't worry, it is quite easy to learn and implement into your project.

PHP Variables from URL

Are you facing problem passing PHP variable from URL? Don't worry, it is quite easy to learn and implement into your project. Remember one thing if you are passing unnecessary information in the URL is fine but if you are passing confidential information like password or username in the URL, It would be dangerous for you. Let see both the example : 

Example 1 : (page1.php)

<html>

<head>

<title>Sending Variable from URL</title>

</head>

<body>

<a href="page2.php?myname=Suraj Mittal&mycompany=roseindia">Send variables from URL</a>

</body>

</html>

page2.php

<?php

$uname = $_GET['myname'];

$cname = $_GET['mycompany'];

echo "My Name is :  ".$uname."<br/>";

echo "My Company name is :  ".$cname;

?>

The output of the above example is : My Name is : Suraj Mittal
                                                        My Company name is : roseindia.

 

In the above example you can see that in the first page we made a link and passed the variables in the URL. It means if you click the link you will go to the next page and your whole information will appear in the url.  But sometime it will create problem for you to show your information in the URL. How see the example below :

 

Example 2 : Page1(login.php)

<html>

<head>

<title>Login Page</title>

</head>

<body>

<form action="welcome.php" method="get">

<table width="300" align="center" cellpadding="2" cellspacing="2" border=""1>

0

<tr>

<td>Username :</td>

<td><input type="text" name="userid" /></td>

1

</tr>

<tr>

<td>Password :</td>

2

<td><input type="password" name="passid" /></td>

</tr>

<tr>

3

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

</tr>

</table>

4

</form>

</body>

</html>

5

Page2:(welcome.php)

<?php

$username = $_GET['userid'];

6

$password = $_GET['passid'];

echo "Welcome ".$username."!"."<br/>";

echo "Your Password is ".$password.".";

7

?>

The output of the above example is : Welcome [email protected]!
                                                                   Your Password is srmt123.

 

8

In this example, we used two files first one is your basic html code file and the second one where we send the information through $_GET method. By mentioning method in the form we can transfers the information from one page to another. Here, we use $_GET method to send information to the other page. Note that $_GET method send the whole information in the above URL and it is not advisable to use this method because anyone get your information through URL. For example: If you are signing up for any side and you fill your username and password and submit the form. At that moment your personal information shown in the URL and anybody can take it from there. It might be shown like that : 

http://localhost/phpVariable/php-variables-from-url_2.php?userid=surajmittal%40roseindia.com&passid=srmt123&submit=Submit.

Ads