This basic article will show you how to get values out of the URL with PHP.
Getting a variable out of the query string is one of the most basic operations in PHP. In fact, the query string variables are referred to as “superglobals“, which mean they are always available anywhere in your code.
If you haven’t used a server-side language before, or if you have only tried to get query string variables using JavaScript, this is much easier.
To access any variable in the URL, just type $_GET["itemname"]
where itemname is the name of the item you want the value for.
Example Usage
The following example will print the word Google
on the screen.
// Our example URL is http://solidlystated.com/?page=1&source=Google echo $_GET["source"]; |
Watch Out For Errors
If the URL does not contain the GET variable you attempt to call, your script will throw an ugly error.
Therefore, always check for it’s existence first!
// Our example URL is http://solidlystated.com/?page=1 if( isset($_GET["source"]) ){ echo $_GET["source"]; } |
That’s it! If you have more advanced questions about the $_GET superglobal or the query string, feel free to ask a question below.
Hello!
I have few query that i will make to a string.
I have about 3 on “type”, and i want, lets say hello hello2 och hello3.
i want hello to be db=1 and hello2 to db=2 and hello3 to db=3.
Thanks in advance!
I would have answered this if I could understand the question.
Hey,
I’m quite new to PHP.
I want to make a game where the player scans QR codes, and they reveal a URL, which registers that they did something on my game.
I was wondering how I could do it?
There are challenges to my game that are made up of several tasks, I’d like to be able to detect what task they have completed and for what challenge for example: http://www.mywebsite.com/?challengenum=1&tasknum=1
Would I make up a bunch of if statements for each challenge and more ifs inside that for the tasks? I would also like it to record the result to the database too.
Thanks
Having a ton of IF statements for a bunch of possibilities would be bad architecture. Additionally, any time you deal with more than 3 or 4 items, you should use a SWITCH statement. However, since you aren’t dealing with a short static list, you should put all those conditions in a database table.
For instance, you could have a table called Challenges that has a 2 integer fields (1 for challenge number and 1 for task number) and a 3rd field that holds an instruction, message, or whatever you want to return.
Finally, since you are new, be very careful of taking user input (from the URL) and using it with your database. Your URL variables absolutely must be sanitized before you use them in a database query.
PS- also, you will want to be more creative with your variables besides 1,2,3 or people could just enter those by hand and immediately complete all your challenges… a random hash for the task would be good. Something like &tasknum=a8bw03mnms2x1!
Thanks for the quick reply. I’ve used switch instead of all those if statements and now my parameters are like this http://www.mywebsite.com/?challengenum=dk48fhrdiwq1D&tasknum=Wed04fN63Cvp0 and everything seems to be working fine.
I’m not too clear on the database structure though. At the moment I have a table called challenge 1 with 4 fields
‘userid’ – auto increment created when a person registers
‘task1’ int
‘task2’ int
‘task3’ int
I just update the task to 1 when it has been completed.
I have another question, would I be able to save the time and date when the player completed a task? and how about recording when a player logs in and out and the duration that their session lasted? If I could do that it would be very useful to me.
Thanks
You should be doing those things anyway. It’s always good practice to have timestamps on database records and you don’t even have to build code around it. I totally recommend this PHP & MySQL book to newcomers, unless you already have a text.
I’m guessing you are using MySQL here, meaning you could add a TIMESTAMP column and set it to automatically record the time when the row is updated. You could also have one column for “last modified” and another for “created on” (which you would pass the value “NOW()” with your insert).
You should also have tables for users and logins for auditing purposes. After you have built many apps/sites that have users and logins, you get a feel for these things.
As for recording the “duration of their session,” that would not be a beginner-type of task. Many people try to use JavaScript to manage sessions, particularly for timeouts. Recording how long someone is doing something is subjective- how do you tell when they are done? do they have to close the page? what if they leave it open and move to another tab? The only thing you can be sure of is when someone sends a request to the server.
hey, I am new to php and currently studying it from a book. In the form handling topic, I have read that after when the form is submitted, all the element names become variables with their values as given and those variables become available throughout php code. but when I tried it in my pc on apache xampp server, it generated an error saying “undefined variable”
Hi Skarim, don’t know what the book is trying to say, but your form values are indeed available anywhere.
They are all stored in $_REQUEST and also in $_GET or $_POST, depending on your form method. They are “superglobals” and are available anywhere. You can access it like $_POST[“firstname”]
Cool info(s)