PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Its used to insert some content but that doesn't really matter, line 18 as said above is the first line. This code works fine once the id has been stated but I need an amendment to make it work when no id is specified.
I'm not a big fan of the "@" operator myself, but in this case using it seems to make sense. Accessing undefined index is not an error in a strict sense (compare to other scripting languages).
<?php
if(isset($_GET['id'])) {
// This really needs to be looked at a lot more closely
$id = $_GET['id'];
$inc_file = 'php/' . $id . '.php';
if(file_exists($inc_file)) {
include $inc_file;
} else {
include 'php/error.php';
}
echo $id;
} else {
include 'php/home.php';
}
?>
should not be used under any circumstances. ctype_ (or equivalent regexp) check is mandatory.
I agree with the checking. I would not include like that myself. However, suppressing an error/warning/notice is not the same as handling it. Throwing an error suppressor in front of the undefined index still results in an undefined index... you just don't see it. Checking to see if the array val is set is totally different than ignoring the fact that your code has errors.
Ok guys I understand the code aint very good, but I don't understand. I changed my code to use isset now, but should I still use ctype_ and how would I implement this. My code now looks like this:
if(isset($_GET['id'])){
$id = $_GET['id'];
$file = "php/$id.php";
if(is_file($file)){
include($file);
}else{
echo "<p class=\"center\"> The page: <span class=\"bold\">".$_GET['id'].".php</span> you requested is unavailable at the present time please try refreshing the page or try again later, your error has been logged and the problem will be fixed shortly.</p>";
}
}else{
include("php/home.php");
}
what are the page names going to look like? If they are pure alphanumeric (literally along the lines of [a-zA-Z0-9]) then you can use ctype_alnum() to check the value.
<?php
if(isset($_GET['id']))
{
$id = $_GET['id'];
if (ctype_alnum($id))
{
$file = "php/$id.php";
if(file_exists($file))
{
include $file;
}
else
{
echo '<p class="center"> The page: <span class="bold">' . $id . '.php</span> you requested is unavailable at the present time please try refreshing the page or try again later, your error has been logged and the problem will be fixed shortly.</p>';
}
}
else
{
include 'php/home.php';
}
}
?>