Jump to content

Using $_GET

- - - - -

  • Please log in to reply
7 replies to this topic

#1
Bioshox

Bioshox

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 207 posts
Okay so I've been asked in the past how does the $GET function work, it's pretty simple really, but very very hand, I've used it before to generate individual user pages by using IF statements and query's where it selected everything where the GET was equal to the user ID or Username.

Okay so to start you need a simple PHP file, it doesn't require much going into it, just this simple piece of code:

<?php

echo "Hello there".$_GET['name'];

?>

Okay so that's the only code we need, save that as test.php or something and then type this into your browser:


http://www.yourdomain.com/test.php?name=Bioshox


You should then see Bioshox is outputted onto your screen! You can change this to whatever you want using the parameters you want.

This can also all be modified using MOD_REWRITE on your .htaccess file if you did want to create user profiles, so you can modify the way the get function actually shows up in your URL.

#2
An Alien

An Alien

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 260 posts
Can you make the tutorial a bit more by adding on how to do it without typing it out in your address bar please?

#3
codeinsight

codeinsight

    Newbie

  • Members
  • Pip
  • 3 posts
Hello Alien, I'm not sure if this is what you're looking for but I'll give it a try.

Code for the page passing values by GET method:
<?php

  $foo = 10;

  $bar = 15;


  echo "<a href='passedbyget.php?foo=" . $foo . "&bar=" . $bar . "'>Pass values using GET</a>";

?>

Code for passedbyget.php (retrieves values using GET)
<?php

  echo "The sum of the values passed by GET is " . $_GET['foo'] + $_GET['bar'];

?>

Btw, this is my first post. :D

#4
An Alien

An Alien

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 260 posts
Well, I meant kinda like input and output, but that is kinda what I was looking for.

Helpful first post! Welcome to CC.

#5
codeinsight

codeinsight

    Newbie

  • Members
  • Pip
  • 3 posts
Oh, okay. Try this one then. :-)

Code to pass form values using GET.
<form name="myform" method="get" action="passedbyget.php">

First Name: <input type="text" name="firstname"/>

Last Name: <input type="text" name="lastname"/>

</input type="submit" name="submit" value="Go"/>

</form>

Code to retrieve form values using GET. (passedbyget.php)
<?php

echo "Hello, " . $_GET['firstname'] . " " . $_GET['lastname'] . "!";

?>


#6
MIMi King

MIMi King

    Newbie

  • Members
  • Pip
  • 5 posts
get is a form submission method... you can either use post or get... to retrieve post use $_POST , for get use $_GET ... for both of them you can use $_GLOBAL!!!

#7
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200

MIMi King said:

get is a form submission method... you can either use post or get... to retrieve post use $_POST , for get use $_GET ... for both of them you can use $_GLOBAL!!!

$_REQUEST is what you will want to use, $_GLOBAL will access the global scope variables for example within a function.

Also, you should always sanitize your $_GET requests:
echo "Hello there".htmlspecialchars($_GET['name']); 

Otherwise nothing stops them from sending page.php?name=<script>malicious script</script> and forcing you to run it.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#8
webcodez

webcodez

    Programmer

  • Members
  • PipPipPipPip
  • 149 posts
Agreed, always make the data from user input secure by either using htmlspecialchars ( for output on screen ) or mysql_real_escape_string ( for database input ). It's easy but vital.

As for the tutorial: a bit short but it shows the principal. You might want to add to it that you can add various GET variables through the url of a PHP webpage by adding &, e.g.:

url.com/file.php?getvars

->

url.com/file.php?var1=value1&var2=value2&var3=value3 ... etc ...




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users