Jump to content

question on include command


Darkmatter5

Recommended Posts

Here's my code

<?php
ob_start();
session_start();

$page=$_GET['page'];
if(session_is_registered('member_id')) {
	include '$page';
} else {
	header("Location: index.php");
}
ob_end_flush();
?>

 

If I have this code

<a href='checkLogin.php?page=res_admin.php'>SITE ADMIN</a>

in a page and I put an echo of $page in the top code. res_admin.php is outputted. So why am I getting the following error with the code as is?

 

ERROR

Warning: include() [function.include]: Failed opening '' for inclusion (include_path='.;C:\php5\pear') in C:\wamp\www\ecabinet\checkLogin.php on line 7

Link to comment
https://forums.phpfreaks.com/topic/113812-question-on-include-command/
Share on other sites

Do not use variables within single quotes. PHP will treat variables as normal text within single quotes. Put simply remove the quotes.

 

Also do not use session_is_registered or session_register functions, they are depreciated. A replacement for session_is_registered is isset($_SESSION['your_variable'])

Okay one last question I think.  I've found that if I do

$pagepath = "C:\wamp\www\project";
$page = $pagepath . $_GET['page']));
echo $page;

 

The "\" in $pagepath aren't recognized. If I try "/" php takes it, but reports it can't find the path as it's now using "/" instead of "\" when going to a directory, so how can I get around this?

PHP sees a \ as an escape character. When using \ in a file path do \\ instead. You can use / in file paths too.

 

Examples:

$path = "C:\\wamp\\www\\project\\";
$page = $path . $_GET['page'];

// OR

$path = "C:/wamp/www/project/";
$page = $path . $_GET['page'];

No matter what I do I can't get the res_admin.php file to include.  I've added C:\wamp\www\project to my php.ini file, still nothing.  I've tried

<?php
ob_start();
session_start();

$page = $_GET['page'];
$path = "C:\\wamp\\www\\ecabinet\\";
$pathpage = $path . $page;
if(isset($_SESSION['member_id'])) {
	include $pathpage;
} else {
	header("Location: index.php");
}
ob_end_flush();
?>

 

Still nothing.  Always the same error a listed in my original post on this topic.  Anyone know what's going on?  Am I going about protecting my pages completely the wrong way?

What happens when you echo $pagepath? Also do not modify the include_path directive, always it leave as default. Post the error message in full here (i know you posted it before, but it may have changed since).

 

I cannot see why the above code is not working.

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.