...

View Full Version : confused with *.php?=*



CodilX
05-30-2007, 10:26 PM
Hi there,

I'm a bit confused, on how to create a webpage that uses the 'index.php?=somethinghere' thing?

What's it called? And how do you use it?

aedrin
05-30-2007, 11:10 PM
They're called GET variables.

PHP provides you with an array called $_GET (it's a superglobal, meaning it's available everywhere and automatically).

So if you have the URL index.php?value=test.

Then this:



echo $_GET['value'];


Would echo: test

CodilX
05-31-2007, 01:10 PM
Thank You.

And how do websites work with let's say

...com/index.php?p=home
...com/index.php?p=about
...com/index.php?p=contact

etc?

that is, how do you make a redirection to a certain url depending on the $GET Variable?

Pennimus
05-31-2007, 01:47 PM
Those types of sites are usually pulling the information from a database, not redirecting. The index page checks what the value of p is, then retrieves the relevant information from the database. It would be possible to construct a site using this type of URL structure without having a database, but it would be pointless.

A very simple example:



$page = $_GET['p'];

// (Connect to database)

mysql_query ("SELECT * FROM table WHERE page = '$page'") or die (mysql_error());

sir pannels
05-31-2007, 02:01 PM
TIt would be possible to construct a site using this type of URL structure

.. don't forget its also possible that urls of that structure could be navigating a templating system such as SMARTY of a custome built template system... it can include files and set directorys/themes in the same way it can call information from a database :D

mlseim
05-31-2007, 02:38 PM
CodilX ...

... and there is also something called "PHP Includes". Includes are often
used to pull information from other pages, other sites, text files, etc.

Maybe an example would be easiest to describe ...

For example, in the site below, there are many text files that the client can
edit using a really basic text editor. The website is one PHP script (page.php) that
uses page codes to determine which text file to display (as content). Move
mouse over links to see what the URL looks like.

http://www.palosantodesigns.com/

So, when the PHP script sees p=2 (for example), it knows which text file
to grab and uses that text file as the content. The navigation (which is
also a PHP script include), adjust according to that variable also.

This type of site allows for dozens of pages without static pages. The site owner
who knows very little about HTML, can go in and edit text files (the content), making
simple changes, additions, corrections. This requires no fancy databases (no MySQL).
It's a rather simple, no frills content management, dynamic site.

EDIT:
There have been discussions about SEO using PHP variables in URLs ...
I've never really had a problem with this. Even a search with part of the name,
which ends up with over one million results ... falls in the first 5 hits:
http://www.google.com/search?hl=en&safe=off&q=palo+designs&btnG=Search

Pennimus
05-31-2007, 02:41 PM
Re: above posts - you're right of course, although it could be argued that those are essentially primitive forms of databasing. I should have mentioned that using MySQL is just one possible form of storing data for retrieval by a dynamic page.

the-dream
05-31-2007, 07:02 PM
Maybe, using includes someting like:




<?php
$page = $_GET['p'];

if($page == NULL) {
include('home.php');
} elseif($page == 'contact') {
include('contact.php');
}
?>

whizard
05-31-2007, 07:23 PM
It would make more sense (and be more extendable) if you did this:


<?php
$page = $_GET['p'];
if($page == NULL)
{
include('home.php');
}
else
{
if(file_exists("$page.php"))
{
include("$page.php");
}
else
{
include("notfound.php");
}
}
?>

HTH
Dan

aedrin
05-31-2007, 07:41 PM
if(file_exists("$page.php"))
{
include("$page.php");
}

If the host has external URLs allowed for fopen then this is a security hole. You need to include some sort of path. Either relative or absolute.



if(file_exists('./' . $page . '.php')) {
include('./' . $page . '.php');
}

CodilX
05-31-2007, 07:55 PM
thank you sooo much! that's what I was looking for. thank you all

whizard
06-01-2007, 01:33 AM
If the host has external URLs allowed for fopen then this is a security hole. You need to include some sort of path. Either relative or absolute.


ahh. Thanks

Dan



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum