Problems with HTTP_POST_VARS
Moderator: General Moderators
Problems with HTTP_POST_VARS
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
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
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
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
Try:
before you deal with the contents of $_GET["p"].
Code: Select all
<?php
if (empty($_GET["p"])){
$_GET["p"]=1;
}
?>
Last edited by patrikG on Wed Mar 17, 2004 6:23 am, edited 1 time in total.
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...
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...
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'];
}
?>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
}
?>- SilverMist
- Forum Commoner
- Posts: 65
- Joined: Tue Mar 02, 2004 2:06 pm
- Location: Canada
- Contact:
Here is a script I dug up. Everything is stored in a single file.
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!
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";
?>