News:

Bored?  Looking to kill some time?  Want to chat with other SMF users?  Join us in IRC chat or Discord

Main Menu

help with making query strings

Started by JayBachatero, May 25, 2005, 09:58:54 PM

Previous topic - Next topic

JayBachatero

im faily new to php and im having a lil trouble wit this piece of code

<?php
$file $_GET["page"];
  
if (!isset($file))
  
{include"main.php";}
  
else {
include "$file.php";}
?>


what i want it to do is if the page is not found that it will display page not found instead of this example error
Warning: Failed opening 'main.php' for inclusion (include_path='.:/php/includes:/usr/share/php') in /home/httpd/html/www.videowoa.com/New Site/index.php on line 55
Follow me on Twitter

"HELP!!! I've fallen and I can't get up"
This moment has been brought to you by LifeAlert

Jerry

try include('filename.php'); instead and for organization sake, } should go on the next line.


- Jerry
Find me on:
Facebook
Twitter
PlanetSMF

"If all you look for is the negative in things, you will never see the positive."

Christian A. Herrnboeck

Hi,

Try this code, it should work better for you. However, I don't understand the logic, as you currently have it, if yourdomain.com/filename.php?page=bar is the url, then it'll open main.php. If the get variable is not set, then you will open up the file?

<?php
//I'm guessing you are trying to do an action array... if this is the case, you should do something like this.
$file $_GET["page"];
  
if (!empty($file))
  
{
  
include('main.php')
  
or die('Could not open file, <b>main.php</b> for reading.');
  
}
  
else 
  
{
  
include($file.'.php')
  
or die('Could not open file, <b>'.$file.'.php</b> for reading.');
  
}
  

?>


I think this code is better; It will 1) check to see if the name attached to the query string actually exists as a file. 2) Attempt to include that file. 3) If there is no query string, it will open main.php:
<?php

//Let's first get the $_GET var...
$file $_GET['page'];
//ok, if there is a URI param we should...
if(!empty($file))
{
//yes, that is right; check to see if the file exists.
if(file_exists($file))
{
//ok, it does, so include it.
include("$file.php")
or die("$file.php could not be opened because of this error: <b>$php_errormsg</b>.");
}
else 
{
//so it doesn't? let's output an error then!
die("$file.php does not exist!");
}
}
else 
{

//so the file parameter isn't set, let's open up main.php in this case.
include('main.php')
or die("main.php could not be opened because of this error: <b>$php_errormsg</b>.");
}
?>


Hope this helps,
Christian


Farmers:Producing food for the world!

JayBachatero

thanks fm-2 the second one did the job thanks agen.
Follow me on Twitter

"HELP!!! I've fallen and I can't get up"
This moment has been brought to you by LifeAlert

Advertisement: