PHP Tutorial on $_GET with Stone River eLearning
PHP is most effective and useful when it is used to take the information from the users and then use those details to authenticate a particular user or to get access to a website or any other services provided by a particular organization. Why haven’t we discussed it yet? Well, the prime reason why we did not discuss how values are fetched from the user because it is very important to get a grip of the basic concepts and functionalities of PHP before we shift towards the more influential aspects of the language. What is the first thing that you do when you visit an ecommerce website or any social networking website? You register with the website and during the registration process; you have to give your basic details and then later you can log-in to the website using the credentials that you get. Have you ever wondered how the website get your details when you click the submit button on the browser? If you think about it from a layman’s perspective, it is like your data getting delivered to a database server that is located millions of kilometers away with just a click of the submit button. Isn’t it amazing? Well, you know that it is happening with the help of PHP, but the bigger question is how? The answer to that is by using the $_GET and the $_POST. In this blog, we will discuss about the $_GET and decide how it stands as compared to the $_POST method.
What is $_GET and how is it used?
The $_GET method is used to deliver information from a PHP form. The $_GET appends the users’ information to the page URL, where in each detail is separated by a ‘?’ symbol.
Eg:
<?php if( $_GET["name"] || $_GET["age"] ) { echo "Welcome ". $_GET['name']. "<br />"; echo "You are ". $_GET['age']. " years old."; exit(); } ?> <html> <body> <form action="<?php $_PHP_SELF ?>" method="GET"> Name: <input type="text" name="name" /> Age: <input type="text" name="age" /> <input type="submit" /> </form> </body> </html>
Note:
- There are quite a few things that can be observed from this example of the $_GET statement. For starters, the method = “GET” in the form section defines the type of delivery option that PHP will use in its webpage.
- Notice how the variables in are accessed using the $_GET in the PHP section of the code.
- User information in the form of variables can also be delivered to any other PHP file. The only change in the above code will be the inclusion of an action =”xyz.php”, where xyz is the name of the PHP file where the variables need to be delivered.
- We have already talked that on using the $_GET option, the user data will reflect in the URL of the web browser once the submit button is clicked. The URL of the next webpage will look something like:
http://www.test.com/index.htm?name1=value1&name2=value2
Where value1 and value2 are the user information and name1 and name2 are the names of the variables they are fed into.
- The problem with using the $_GET method is the security threat that it poses as the user information is directly displayed in the URL of the webpage and the possibilities of injections are pretty high. $_POST is considered to be a safer and better approach to deliver values.
- $_GET finds great utility for debugging purposes, primarily to check if data is actually getting transferred or not.
GET YOUR FREE PHP EBOOK!