Jump to content

@inlcude functions


eZe616

Recommended Posts

Not sure if it is the right forum but, here goes:

 

I'm trying to build a website using the "include" function, here's the code:

 

<?php if ($page == "") { include "index.html"; } else { include "$page.html"; } ?>

 

Now the code seems to be right, but it won't work on my home comp server that I installed a few days ago...

 

I'm Using Apache 2.2.4, PHP 5.2 and MySQL, 5.0.27.

 

Can it be possible that the settings of the PHP wont make it work?

 

If so how can I maki it so that i works

Link to comment
Share on other sites

Yes..tht's how I want to do it. tnx, the change makes it work now. How can I run more checks againts the $page then? I'm kinda new to php coding so help is appreciated.

 

Also when I try a modified version as :

 

<?
$default_page="news";
if (!$page) {
$page="$default_page.html";
} else {
$page="$page.html";
}
if (!@include("$page")) {
include("404.html");
}
?> 

 

It won't work at all

Link to comment
Share on other sites

Nope...No Errors, nothing..its just blank were the file is supposed to be included

 

This is my current code

 

<?
	$default_page="index";
	if (!$page) {
	$page="$default_page.txt";
	} else {
	$page="$page.txt";
	}
	if (!@include("$page")) {
	include("404.txt");
	}

?>  

 

and my links look likke this...

 

<a href="index.php?page=padu" >

 

and the files that are to be inluded are all .txt files

 

Link to comment
Share on other sites

 

Try this:

 

<?php
error_reporting(E_ALL);
extract($_GET);

$page = isset($page) ? $page : 'index' ;

if (!(@include("$page.txt"))) {
	include '404.txt';
}
?>  

 

Yes...Thank you, it works... Since I'm new to php, is it secure like you said in the first post

 

I assume you're passing $page via GET, like so: index.php?page=something.html. Right? If so, you need to read about register_globals. Also, you need to run more checks against $page; malicious users can use ../ to access parent directories.

 

Link to comment
Share on other sites

This is better:

 

<?php
$page = $_GET['page'];
$page = ($page !== '') ? $page : 'index' ;
$page .= '.txt';
$page = preg_replace('%(?:\.\./)+%', '', $page);

if (!is_dir($page) && file_exists($page)) {
	if (!(@include($page))) {
		include '404.txt';
	}
}
else {
	echo 'File does not exist';
}
?>  

 

Update: Actually, making a list of valid pages is better.

Link to comment
Share on other sites

../ means go up a directory in the path. This line removes any occurrences of these characters. The %'s are used as delimiters and the . is escaped because it is a metacharacter in regex. The (?: ) are non-capturing parentheses which group these characters together, and + indicates one or more. I recommend looking through the regex links in my signature.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.