Forum Moderators: coopster

Message Too Old, No Replies

require based on GET param

         

mtmtmt

5:35 pm on May 9, 2006 (gmt 0)

10+ Year Member



i have this simple code
<? require($_GET['xhtml'].".xhtml");?>

now when the url doesn't include "?xhtml=somthing" i want it to require another page. how can i do this?

jatar_k

5:45 pm on May 9, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld mtmtmt,

you would need to test for the existence of that GET param first

if (isset($_GET['xhtml'])) {
require($_GET['xhtml'].".xhtml");
} else {
// include something else
}

you may also want to test $_GET['xhtml'] to be sure it isn't empty as well in the same if

mtmtmt

5:55 pm on May 9, 2006 (gmt 0)

10+ Year Member



thanks. maybe you could help me with this...

say someone goes to 'http://www.url.com/?xhtml=something' but 'something.xhtml' doesn't exist. can i show an error page?

jatar_k

6:04 pm on May 9, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you could use is_file [php.net]

I am assuming they are all real files. Just drop the is_file test in before you require it, if it's false drop to an else that shows an error page/message. Be sure to send proper 404 headers for files that don't exist.

mtmtmt

6:29 pm on May 9, 2006 (gmt 0)

10+ Year Member



I have put together this little code and it seems to work... thanks.


<?
if (isset($_GET['xhtml']))
{
if (is_file($_GET['xhtml'].".xhtml"))
{
require ($_GET['xhtml'].".xhtml");
}
else
{
require('404.xhtml');
}
}
else
{
require('xhtml0.xhtml');
}
?>

jatar_k

6:31 pm on May 9, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



nice

you're welcome ;)

mtmtmt

7:42 am on May 10, 2006 (gmt 0)

10+ Year Member



me again... everything works except when it detects the cookie and skips the splash screen. i cant figure out whats wrong. if the cookies not there it requires xhtml0.xhtml, but if the cookies there it does nothing.

<?
if (isset($_GET['xhtml']))
{
if (is_file($_GET['xhtml'].".xhtml"))
{
require($_GET['xhtml'].".xhtml");
}
else
{
require('404.php');
}
}
else
{
if (isset($_COOKIE['xhtml1']))
{
require('xhtml1.xhtml');
}
else
{
require('xhtml0.xhtml');
}
}
?>

eelixduppy

11:12 am on May 10, 2006 (gmt 0)



Add a little echo to the it like this:

if (isset($_COOKIE['xhtml1']))
{
echo "Cookie exists";
require('xhtml1.xhtml');
}

Just to make sure the cookie is actually set and that its not a problem somewhere else.

eelix

mtmtmt

6:13 pm on May 10, 2006 (gmt 0)

10+ Year Member



lol... the file on my server was 1 letter different (xhtm11.xhtml instead of xhtml1.xhtml)

as you can see in code 1 looks alot like l...

xht11.xhtml - xhtml1.xhtml

eelixduppy

7:06 pm on May 10, 2006 (gmt 0)



It's funny how you did that having the keys you mixed up on opposite sides of the keyboard :)

eelix