...

View Full Version : PHP GET id help



mugenINFINITY
07-22-2008, 10:07 PM
<?php
if ($_GET['id'] == 'page.php')
include_once('page.php');
?>

I have been searching for how to use this code or codes like this for about an hour I am a bit frustrated because I am not sure what I am looking for. Basically I want this format for linking:

index.php?id=page1.html

Does that make sense? I realize I am completely new at this sort of thing and I would greatly appreciate any help.

djm0219
07-22-2008, 10:11 PM
It makes sense though it's rather dangerous to include things based simply on what is passed in the URL. Change what you have to read:


<?php
if ($_GET['id'] == 'page1.html')
include_once('page1.html');
?>


You could also use something like:



<?php
if ($_GET['id'] == 'page1')
include_once('page1.html');
?>


and call it using: index.php?id=page1

mugenINFINITY
07-22-2008, 10:27 PM
<?php $id = $HTTP_GET_VARS['id']; if ( !$id || $id == "" ) else { include "$id"; } ?>

This is another version of the code that I have found that will not work for me.

I tried what you gave me, but how would use that code for multiple pages? I want the layout to be the same and the content to change. Page 2 Page 3 etc...

Many Thanks

PeaTearGriffin
07-22-2008, 11:42 PM
<?php $id = $HTTP_GET_VARS['id']; if ( !$id || $id == "" ) else { include "$id"; } ?>

This is another version of the code that I have found that will not work for me.

I tried what you gave me, but how would use that code for multiple pages? I want the layout to be the same and the content to change. Page 2 Page 3 etc...

Many Thanks

That is for PHP 4 and lower I think, so you'd have to replace $HTTP_GET_VARS with $_GET.

p@p
12-22-2010, 07:58 PM
Or could be like this:


<?php
$phpself = $_SERVER['PHP_SELF'];
switch($_GET['id']) {
case '1':
// This could be include, echo or what ever
include ("page1.php");
break;
case '2':
// This could be include, echo or what ever
include("page2.php");
break;
case '3';
// This could be include, echo or what ever
include("page3.php");
break;
}
?>


Then you called yourdomain.com/index.php?id=1 for page1.php

Fou-Lu
12-22-2010, 08:56 PM
<?php $id = $HTTP_GET_VARS['id']; if ( !$id || $id == "" ) else { include "$id"; } ?>

This is another version of the code that I have found that will not work for me.

I tried what you gave me, but how would use that code for multiple pages? I want the layout to be the same and the content to change. Page 2 Page 3 etc...

Many Thanks


That is for PHP 4 and lower I think, so you'd have to replace $HTTP_GET_VARS with $_GET.

Yes and no; most configurations still allow the usage of registered_long_arrays ($HTTP_*_VARS) except for raw which is still valid. There are a few things to note on these though:

As mentioned they are old. Really old. Superglobals were added in 4.1. That like... december 2001ish? Has it been that long already?
Registered long arrays are not superglobals. In fact, when 4.1 was new we used to develop similar to so for maximum compatibility:


if (PHP_VERSION < 4.1)
{
$_POST = &$HTTP_POST_VARS;
$_GET = &$HTTP_GET_VARS;
...
}

function myfunc()
{
global $_POST;
}

The biggest flaw of course that registered_long_arrays are NOT superglobals and you couldn't create your own. This is why we needed to globalize the superglobals across functions. I'm so glad those days are gone.
Registered_long_arrays are officially deprecated as of 5.3.0, and we're hoping they will be gone in 6.0. That means there is no guarantee that any future release will include them. Current versions may disable the long arrays, but they are enabled by default.
Registered_long_arrays are not related to the superglobals. They draw the same data, but they are not written as references. Any changes in one will not effect the other during the scripts life.


Anyway, I'm the lazy one here so I'd probably do the same like so:


<?php
$page = $_GET['page'];
$aAllowed = array('page1', 'page2', 'page3');
if (in_array($page, $aAllowed))
{
$page .= '.php';
if (file_exists($page))
{
require_once $page;
}
}

The only thing I'd probably do is make sure that the provided page doesn't include any /'s in it.

When I write my own stuff I have a controller who passes all requests through filters. So mine is somewhat similar to the integer approach by p@p where the filters make determinations of what to do, and these are dynamically generated so it doesn't have the actual switch.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum