FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


PHP error page, die()





Relentless
Let me explain what I have got before showing the script.

I have made my own blog, so far with the help from Sonam.

now I have managed to get a script that inbeds into view.php so that if the url is view.php?post=1 it it includes post 1 into where i say.
Code:
 
<?php
    $page = $_GET['post'];
        if (!isset($page)){
    $page = "home";}
        include('posts/' . $page . '.php');
?>

Now this script works fine! However if someone was to put a number or letter in place of the 1 and that page doesnt exsist then I get this error

Code:
Warning: include(posts/2.php) [function.include]: failed to open stream: No such file or directory in /mounted-storage/home97c/sub003/sc61033-NOGM/Sub_Domains/s.DanielKendal.com/clientsarea/blog/view.php on line 61


so I had a look on the net to find a solution and I found this
Code:

<?php
if(!file_exists("welcome.txt"))
  {
  die("File not found");
  }
else
  {
  $file=fopen("welcome.txt","r");
  }
?>


So I modified it to work with mine like so...
Code:

<?php
       $page = $_GET['post'];
           if (!isset($page)){
       $page = "home";}
if(!file_exists('posts/' . $page . '.php'))
{
die('File not found');
}
else
{
   include('posts/' . $page . '.php');
}
?>


No then this works and everything else works great!!!! HOWEVER if it does have to use the "File can not be found" after that line appears on my website not HTML follows.... I mean it just stops dead. after that sentence, I checked the source code and it has no more HTML after then...

Can somebody please help me out. I want the error to say "The Post has either been deleted or not created yet. Please click on the other posts link (top left) to view the latest post"

Cheers Daniel Cool
ProwerBot
Using die() will stop the page from loading beyond that point. Try using echo instead.
sonam
Like ProwerBot wrote echo is better solution but in some situation you cannot use echo statment. For example, when you have image background, how I know, image come over this message, or if this message come up on top of your xhtml then some browsers can ruine your work and display something ugly. In that case is better to define in script one variable or even more include some error page.

Code:
<?php
       $page = $_GET['post'];
           if (!isset($page)){
       $page = "home";}
if(!file_exists('posts/' . $page . '.php'))
{
$error = "error";
 include('posts/' . $error . '.php'); 
}
else
{
   include('posts/' . $page . '.php');
}
?>
Relentless
Sonam once again you are a legend! PHP master!!!! Thanks alot! you have helped me so much lately!!
Very Happy
sonam
Relentless wrote:
Sonam once again you are a legend! PHP master!!!! Thanks alot! you have helped me so much lately!!
Very Happy


Thank you. Wink
Related topics
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.