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
https://forums.phpfreaks.com/topic/38656-inlcude-functions/
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
https://forums.phpfreaks.com/topic/38656-inlcude-functions/#findComment-185581
Share on other sites

How can I run more checks againts the $page then?

 

You might want to make sure that the file exists.

You should also check for/remove any directory changes:

 

$page = preg_replace('%(?:\.\./)+%', '', $page);

 

It won't work at all

 

Specifically, what doesn't work? See Example 16.8.

Link to comment
https://forums.phpfreaks.com/topic/38656-inlcude-functions/#findComment-185624
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
https://forums.phpfreaks.com/topic/38656-inlcude-functions/#findComment-185753
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
https://forums.phpfreaks.com/topic/38656-inlcude-functions/#findComment-185770
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
https://forums.phpfreaks.com/topic/38656-inlcude-functions/#findComment-185780
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
https://forums.phpfreaks.com/topic/38656-inlcude-functions/#findComment-185850
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.