dalehay.com
Search Engine Optimization

GET String in PHP

Digg! This Page
This tutorial will show you how to use the GET string. If you do not know what a get string is, then hopefully you'll know by the end of this tutorial.

About GET Strings
GET strings in PHP are basically the value within the address bar of a website. For example, if you went to a website and it had the address like this
http://www.dalehay.com/story.php?id=1
Then the GET string will be called id and the value will be 1.

Step 1
To pull the value from the address bar all you need to do is firstly open your PHP tags.
<?php

?>
Step 2
Now add a print command (to show the value) and then add the GET string after it
print $_GET['id'];
This will show the value of 1 (if the PHP file address ends in ?id=1) Step 3
Using this simple GET string can be used to make a mini-dynamic website, for instance you could use this to have several pages running through one page.
<?php
echo "Welcome to my site"";
include("./$_GET[page].php");
echo "<hr>Come again soon.";
?>
Then have links on your site goto index.php?page=news, create a page called news.php and it'll be shown if a visitor goes to that page.
Example
Below is a very simple (full) example of using this.
index.php
<html>
<head>
<title>My Site</title>
</head>

<body>
<h2>My Site, Welcome!</h2>
<h4>Menu: <a href="./index.php">Home</a> - <a href="./index.php?page=news">News</a> - <a href="./index.php?page=contact">Contact</a></h4>
<?php
if($_GET[page]) {
include("./$_GET[page].php");
}
?>
<hr>Copyright, Blah Blah, 2008</body>
</html>

news.php
<b>Website Updated</b>
Its a new year and I have a new website, if you like it give me some feedback.

contact.php
<b>Contact</b>
You can contact me either by emailing me ( blah @ blah . com ) or phoning me ( 1234 567890 )
Extra Addition
Using the above code, you'll notice that I have added an IF tag, this is because if no GET value is set then it tries finding an empty file that obviously cannot exist, so it returns an error, however sticking this IF tag around the include() statement will stop it from returning an error.
if($_GET[page]) {

}
Basically just having the IF statement, in plain English terms saying "If the GET value page has any value, except blank, then proceed." Nice and easy!

Comments

Comments:

Add your comment...

Your Name:

Your Email:

Your Comment:

Enter This Number:

(73943)


Note: Your IP address is saved into the database when you submit a comment. Any type of threatening behaviour will result in your ISP being contacted and legal action being taken place!





Latest News Stories
» Not good...
» Internet Explorer 8 Problems
» October
» Relaxtion by Dale Hay
» BBC Weather Revamp
» Dog Singing
Top Viewed Stories
» PHP Mini Message Board Tutorial (4,211)
» Rare McDonalds Monopoly 2008 Tickets (3,056)
» PHP Search Engine Tutorial (2,271)
» Helen Willetts Pregnant? (1,399)
» QI Series 6 on BBC One (900)
» Create your own Image Uploader (873)
Most Commented Stories
» PHP Mini Message Board Tutorial (33)
» Create your own Image Uploader (18)
» I like my DuMP (15)
» I'm on Wikipedia! (7)
» Chocie Munchin Niece (6)
» Life on Mars... (6)