Php Get and Post
In php you have alot of functions that you use many over and over again, but the 2 i've come to use
the most are the '$_GET' and '$_POST' functions. I have just mislead you by calling them functions
though since they are not. They are part of the group of called 'superglobal variables'. 'Superglobal
variables' are predefined variables in PHP, and they are available in all scopes throughout a script.
Here is a list of all the global variables.
*note - I call these functions in the tutorial because its easier to say then superglobal
What Is It For?
The get and post variables are for sending data from one page to another. For example you have a form with a textbox named 'name'. When you click the button in the form, it will pass what ever data is in the textbox to the next page and using get and post you must retrieve it. This can come in handy in many scenarios, that involve user information needing to be send into a database.
My Personal Method
I don't know if everyone does it like this, if its just me, or if a few others do or not, but I use get and post each in a certain way though they can be used in the exact same manner as one another. In my opinion putting data inside the url (ex. 'localhost/index.php?user=info&pass=moreinfo') looks exetremely tacky and unorganized, as well as hard to memorize. I like my users to understand what the url says as well, i want them to be able to come back to an exact page easily. So i use post when passing data, but when handling stuff such as a webpage with mulitple outputs i would use get.
Whats The Difference
There is a small but at the same time a very major difference in get and post. When using get all the information being gained must be taken from the url, thus giving you long url names. Though post is completely hidden from all user eyes(comes in handy for passing a password). You may say, 'well why not always use post'. Simple answer there, you can't, post can only be accessed when passing on data inside of a form. Meaning that in order to make your websites better, your gonna have to manage to mix in a bit of both.
We all know that the lecture part of anything is boring but its also always necessary. Alot of tutorials
just give the code instead of explain it. In doing so, leaves the readers confused to what they're doing
and unable to help others with the knowledge. Now that that parts over lets get into the code though.
In this tutorial were gonna be developing a login system, but the system will not be complete until my
next tutorial. In this tutorial were gonna learn the basics of multipages inside of one file, and sending
and echoing data through the server.
Down To Code
Okay to start were gonna need to design the page, lucky for you i've already done that since this is a php
tutorial not a css one i don't wanna stray from what were here to learn.
The most important line in this to allow us to use our get and post effectively.
//Define data here
$HTML .=" <form action='login.php?m=1' method='post'>
<input type='text' name='user' size='25' />
<input type='password' name='pass' size='25' />
<input type='submit' value='login' /> </center></div>
the form tag is what we use, action is what page were going to notice 'login.php?m=1'.
<form action='login.php?m=1' method='post'>
m is the variable inside of $_GET that we will be accessing.
were gonna use $_GET to check if the data has been sent or not.
lets get inside of our first php code area
We were gonna change the code inside of there to this
//Define data here
now that we have gained the value of 'm' and saved it as a variable '$MODE' lets access it to.
$MODE = $_GET['m'];
in the second php code area, lets add this above the other code
run your code in your php server to see the example.
if($MODE != null || $MODE != "")
$HTML .= "<p> Your data has been sent to this form</p>";
type in some random info into the 2 textboxs and click submit.
notice the url changes but the only difference was 'Your data has been sent to this form' was
output to the top of it.
excellent now we know whether or not the data is sent but, we haven't accessed the data that the
form passed. To do so we will be making use of $_POST.
Lets go to our first php code area and under the code already placed lets add this.
its the same exact syntax as get
$user = $_POST['user'];
$pass = $_POST['pass'];
now lets output our user info to ourself by adding this in our if function
run your code and see the effect of the script.
$HTML .= "<p> Your username is " . $user . "</p>";
$HTML .= "<p> Your password is " . $pass . "</p>";
This isn't a login system though because it doesn't check another username/password for a match
since this is just a tutorial were gonna use some custom defined user info.
add this under your defines
them just sitting there doesn't show us anything were gonna need to use if functions as comparisons
$loguser = "username";
$logpass = "password";
to make sure the data matches if it does match, then it will login, if not state the error.
lets start by checking to make sure no fields were left blank replace this with the code inside the if
the code is explained in the comments, and there we have a functional login system, keep looking around for my next tutorial, which will explain creating the session to keep them logged in and use mysql to store accounts and retrieve info from there.
if($user == null || $user == "")//Checks if username is empty or not
$HTML .= "You left your username blank<br />";//outputs if username is empty
else if($pass == null || $pass == "")//Checks if password is empty or not
$HTML .= "You left your password blank<br />";//outputs if password is empty
else if($user != $loguser)//Checks if username is correct
$HTML .= "Your username is incorrect<br />";//outputs if username is incorrect
else if($pass != $logpass)//Checks if password is correct
$HTML .= "Your password is incorrect<br />";//outputs if password is incorrect
else//If all ifs fail then login info is correct and login succeeds
$HTML .= "Login Successful<br />";