So you would think that I could search up on Google and learn all about URL encoding from all the tutorials, but I have been left seriously confused, and extremely frustrated.

Here is what I need to do:

I need to make a html link that links to a php file. the link needs to carry over a variable to the php file. (for example: mysite.com/php_file.php?variable=22)

Then, in the php file, I need to say something like...

$variable = [Whatever 'variable' equals up there in the URL]

echo "Your variable is...".$variable;

Output:
Your variable is...22


But so far (even after hours of Google searches) I have absolutely no clue whatsoever on how to make this work. I have tried many different tests, but none have worked.

Can someone please help explain this to me? I know it can be done, and thats why it's so frustrating. Thank you so much in advance! :)

You got the general idea.

to send a variable through a url you do as u showed: mysite.com/php_file.php?myfirstvariable=22

now in the php file u r using where you want to retrieve that variable you must $_GET it! : $variable=$_GET;

$variable will now have the value 22

you can use $variable = $_REQUEST;
but you don't need to.
effectively passing variables through the url is just like putting the variable at the top of the document.

if you call the file www.example.com/file.php?variable=22
you can say:

<?PHP
echo $variable;
?>

or if you want mutiple variables such as variable=22 and product=cheese
then www.example.com/file.php?variable=22&product=cheese

<?PHP
echo "product: $product<br />
Quantity: $variable";
?>

Hope this helps :)

Wow, thank you guys sooo much for your help! :D I'm glad I know now

No worries.
Remember to mark as solved if solved :)
otherwise is there anything else you you need help with?

None of what was explored here is url encoding. URL encoding means encoding characters that not safe to be passed in the url, for example a "/" or a "+". When you url encode a url you encode those in hex or decimal format. "/" => %2F => %47.

For example say you passed a directory path as a variable in the url:
domain.com/?secret=/home/user/secret/folder/

This would cause problems if you didn't encode the url. If you HAD to pass this in the url you could encode the "/"'s as:
domain.com/?secret=%2Fhome%2Fuser%2Fsecret%2Ffolder%2F

When you decoded the incoming url you would have a variable with the slashes restored.
Obviously, there are MAJOR security issues with passing a path in the url like this....but it is just illustrative.
More on url encoding: http://www.blooberry.com/indexdot/html/topics/urlencoding.htm

Additionally, if you're using variables from the URL they are accessed from the $_GET array. $_REQUEST should be avoided for this as it works its way down the list of $_GET $_POST etc till it finds an occurrence of what you want.

Being able to access: domain.com/?variable=purple as

<?php
echo $variable; //purple

Relies on a feature of php called "register globals" this was defaulted to OFF in php 4.2.0 DO NOT rely on this being enabled. As of php 5.3.0 this is depreciated. You can read all about it here: http://php.net/manual/en/security.globals.php

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.