Problems with HTTP_POST_VARS

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Problems with HTTP_POST_VARS

Post by mjseaden »

Dear All

I'm currently building a fully encapsulated site using PHP. The code is structured so that the whole site is a single script, index.php, and all of the pages are implemented as separate functions, with each page being called by an URL-posted variable (e.g. p=1, front page, p=2, search page etc.).

I have two problems:

(1) The value of 'p' is unpredictable until it has been defined. Therefore I do not know what to do when the site is first visited, where the variable 'p' is not posted on the URL signifying the page - to explain, if I want the front page, I need to use the address 'index.php?p=1'. However when first visiting the site, obviously the address is simply 'index.php'. How do I detect this and automatically place p=1 to call the front page function?

(2) Previously, I did have the site as a set of separate pages - index.php, search.php, etc. There is a search form on the front page, index.php, which has on it a form with a set of criteria you can select. When the user pressed the 'submit' button, the action was referred to search.php. In search.php, the criteria variables set by the user on the search form on index.php were obtained using the standard HTTP_POST_VARS.

However, now I have the search page all in the same index.php script, to call the search page I use 'index.php?p=2'. When I put this in the ACTION variable of the form enclosing the search form on the front page, it works, however now the HTTP_POST_VARS are coming up as NULL. How do I then effectively communicate these variables from the form on the front page?

Sorry for the heavy-handed explaining, I hope that makes sense to you seasoned programmers.

Thanks

Mark
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Post by mjseaden »

Thanks Patrick, using $_POST has solved the problem of not receiving the variables.

Can anyone help with the first point? How do I get index.php to either arrive automatically at index.php?p=1, or seamlessly redirect to that URL when the site is visited via the address index.php only?

Many thanks,

Mark
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Try:

Code: Select all

<?php
if (empty($_GET["p"])){
   $_GET["p"]=1;
}
?>
before you deal with the contents of $_GET["p"].
Last edited by patrikG on Wed Mar 17, 2004 6:23 am, edited 1 time in total.
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

On question (1)....

You can declare a default value then you need to check that a) it has been declared in the URL and b) if it has, it is not empty.

so...

Code: Select all

<?php
$p = '1';  // declare default page to load if none requested.

if (isset($HTTP_GET_VARS['p']) && !empty($HTTP_GET_VARS['p']))
{
  $p = $HTTP_GET_VARS['p'];
}
?>
You should also do some sanity checking to make sure the page actually exists.

On question (2).....

$HTTP_**_VARS are not super global, so using these within a function will require you to declare them as such.

So inside the function that requires them you can use...

Code: Select all

<?php

function whatever()
{
  global $HTTP_POST_VARS;
  // do something
}
?>
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Post by mjseaden »

Thanks for the help - it's all up and running now.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

:)
User avatar
SilverMist
Forum Commoner
Posts: 65
Joined: Tue Mar 02, 2004 2:06 pm
Location: Canada
Contact:

Post by SilverMist »

Here is a script I dug up. Everything is stored in a single file.

Code: Select all

<?php
extract($HTTP_GET_VARS);
extract($HTTP_POST_VARS);
if ($page != "PAGEHERE1" && $page != "PAGEHERE2" && $page != "PAGEHERE3"){
?>main (index.php) stuff here 
<?php
}
if ($page=="PAGEHERE1")
echo"your stuff that goes in index.php?page=PAGEHERE1";
?>
I don't have trouble with it....However, if you do, just send me a message. I'd be happy to help you with it!
Post Reply