[PHP] - Dynamic Website Error - Need help

Sup3rCow

Active Member
Joined
Nov 20, 2009
Messages
85
Reaction score
0
Hi guys here is a code for a simple dynamic website:

Index:
Code:
<html>
	<table width="70%" align="center">
    	<tr>
        	<td>
            	<h1>My Web Page</h1>
            </td>
        </tr>
    </table>

	<table width="70%" align="center">
		<tr>
        	<td width="20%">
            	<b>
                <a href="index.php">Home</a>
                <a href="?p=test">Test</a>
                </b>
            </td>
            <td width="80%">
            	
				<?php
                $page = $_GET['p'];
					if ($page)
					{
						$path = "inc/".$page.".php";
						
						if (file_exists($path))
						{
							include ("inc/".$page.".php");	
						}
						else
						{
							echo 'Sorry, the page does not exist';
						}
						
					}
					else
					{
						include ('inc/home.php');
					}
					
                ?>
                
            </td>
        </tr>
	</table>

</html>

inc/test.php
Code:
<html> 
This is my test page
</html>

inc/home.php
Code:
<html> 
This is my home page
</html>

Now why does it say "undefined index: p" in the home page, but not the test page? Plz help to remove it thanks. P.S. I know its not so secure I am fixing it, but is struggling with this error and remember I have no MySQL database this is purely PHP.

Thanks in advance
Sup3rCow
 
Last edited:
Nothing wrong with the code.
Works fine on my side.
 
not going to test it , but undefind index p probably means undefined index 'p' :p
you should probably do somthing like this in future , if (isset($_GET['p'])) { $page = $_GET['p']; }
 
ok thanks guys first I though I it was the internet browsers, but both chrome and explorer give the error... I am testing it on a local server can that be the problem?
 
Ewww .. Tables. Seriously should perhaps think of using div instead of tables.
 
Ewww .. Tables. Seriously should perhaps think of using div instead of tables.

LOL... I always use DIVS but when creating this script I wanted to make it easier and quicker so used TABLES instead, but when it is finish will use divs
 
Simples

When you are on the home it is index.php and you use "$page = $_GET['p'];" which is looking for the variable 'p' in the $_GET array, as this is not set it gives you a warning. When you use test you set the variable '?p=test' now you have the 'p' variable set the the GET array and so you made use it.

if (isset($_GET['p'])) { $page = $_GET['p']; }

By doing this, you are telling it to only use the p variable if it is indeed set and hence, avoid your warnings.
 
Last edited:
The reason it works on your host is that in a production environment warning messages will be turned off, it is still best practive to develop with warnings and errors turned on so you can fix them before they go live. To get rid of index.php you will need to use rewrites, you can do this by editing your .htaccess file, something like this.
 
Top
Sign up to the MyBroadband newsletter
X