Jump to content

PHP Include help.


jadedmonkeys

Recommended Posts

Hello folks, It's my first post here.

 

I am currently working on a page where I have made a header and a footer and have a bunch of html text files that I'd like displayed in a DIV called 'stage' for example.

 

since I have only 3 unique php files with their own header/footer and about 50 of these html text files I'd like to figure out a way to get a URL sort of like.

 

example.com/main.php?content=textfile1.html

example.com/nav.php?content=textfile2.html

 

where the php file is the header/footer file with a DIV named 'stage' inside of it and I'd like to display the html in textfile1.html inside the stage div.  I'm thinking of using php include but i dont know how that would work since I'd have to already specify what I want to include in the php whereas in this case I'd like to specify what I want to include from the URL.  I don't know where to do begin on this.  Thank you in advance for your help.

Link to comment
https://forums.phpfreaks.com/topic/201023-php-include-help/
Share on other sites

Try this:

<?php
  if(isset($_GET['content'])){
    $content = $_GET['content'];
  }
  include($content);
?>

 

You really should validate any input coming from the URL or you will get burned.

 

<?php
$valid_content = array('content1.html','content2.html','content3.html'); //put all of the valid filenames in the array
if (isset($_GET['content']) && in_array($_GET['content],$valid_content) && file_exists($_GET['content'])) {
    include($_GET['content']);
} else {
    include('default_contents.html');  // show default page
}
?>

 

Ken

Link to comment
https://forums.phpfreaks.com/topic/201023-php-include-help/#findComment-1054704
Share on other sites

Guess I'm a little lost on what you mean, but do you mean something like this...

 

Needs to be customized to your needs, but it's a start for you.... if you need help sifting through that let me know. Just assuming you can take out of that what you need.

 

URL IS SOMETHING LIKE THIS  ---> /index.php?page=Employee

<?php
				// Check if page has been requested

				if (!isset($_REQUEST['page'])) 
					{ 
					$page = '/main.html';
					} 

				else 
					{
					 // request page here
					$page = $_REQUEST['page'];
					$page = ('/'.$page.'html');						

					} // End if page has been requested

				// Check to see if page exists
				if (file_exists($page)) 
					{ 
					// Page exists

					// Show page
					include("$page");
					} 

				else 
					{ 
					// Page doesn't exist
					echo 'Sorry, the page that you are trying to access does not exist.';
					} // End if page exists


?>

 

Link to comment
https://forums.phpfreaks.com/topic/201023-php-include-help/#findComment-1054707
Share on other sites

Also, you shouldn't use filenames like that in the URL, rather use a switch statement and use related references such as: content=home, instead of content=home.html. It's never a good idea to send too much info about how the files are structured and make it visible for everyone to see...

Link to comment
https://forums.phpfreaks.com/topic/201023-php-include-help/#findComment-1054756
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.