Basic TutorialsBasic Tutorials
Error handling for dynamic inclusion
To detect error in dynamic inclusion, you must try to test whether the file exists. So modify your code to
For the second type of dynamic inclusion, also change code to
Back
To detect error in dynamic inclusion, you must try to test whether the file exists. So modify your code to
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>
<BODY>
<?php
// the following part allows to you call your file without "?"
if(!$z)
include("home.php"); // or whatever you want. make sure it exists!
else {
// make sure it's a file in this particular directory
$z = basename($z);
// make php extension for it
$z .= ".php";
if(is_file($z)) // does it exist?
include($z);
else
print "Sorry, the page doesn't exist!"; // or you can include another file
}
?>
</BODY>
</HTML>
Save it as mod_test3.php. And now when you call mod_test3.php?z=meow, see what happens? :)
For the second type of dynamic inclusion, also change code to
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>
<BODY>
<?php
if(!$z)
// the following part allows to you call your file without "?"
include("home.php"); // or whatever you want. make sure it exists!
else {
// make sure it's a file in this particular directory
// $_SERVER['QUERY_STRING'] gives you everything after
// the question mark
$z = basename($_SERVER['QUERY_STRING']);
// make php extension for it
$z .= ".php";
if(is_file($z)) // does it exist?
include($z);
else
print "Sorry, the page doesn't exist!"; // or you can include another file
}
?>
</BODY>
</HTML>
That's how to you do it :)
Back
Scripts and tutorials © 2003 - 2013 by Ying Zhang. No redistribution without permission!