$x will be null if you do not have globals set. Use $_GET['x'] to access the GET variable instead (or $HTTP_GET_VARS['x'] if PHP<4.1.0).
As for - <% include("$x" + ".html"); %>
Try -
<? include ($_GET['x'] . ".html"); ?>
Hope this helps
$x will be null if you do not have globals set. Use $_GET['x'] to access the GET variable instead (or $HTTP_GET_VARS['x'] if PHP<4.1.0).
As for - <% include("$x" + ".html"); %>
Try -
<? include ($_GET['x'] . ".html"); ?>
Hope this helps
If you still want to have a dynamic first page you can try this
if(!isset($_GET['x'])){
include ('main_content.html');
titleVar = "Main Page";
}
else{// do this if $_GET['x'] isn't empty
//Do case statement
}
I FINALLY got it to work yesterday after posting. I have my title variables working and the content variable working as well. I'm using PHP to build DYNAMIC WEB PAGES!!! WOOHOO!!! Next, Database Driven Web Pages. Thanks for all of your help, Jeremy
Hi Sarah,
I'm very new to php and this board has helped tremendously. My question is, if my page has 100 links and I have a template page does that mean I need to put all 100 urls or potential links into the switch script as stated before? That's seem to take forever. Is there a more efficient way for larger sites?
Thanks so much.
Joseph
<?
if($x=="engineering"){
include("engineering.html");
}elseif($x=="fabrication"){
include("fabrication.html");
}
//and so on and on....
?>
You could try
//template.php
$target_page=$_GET['x'];
$target_page=$target_page."html";
include ($target_page);
.
.
.
//Do something
When you call the script
<a href="template.php?x=engineering">Engineering Page</a>
Hi dustbuster,
Thanks for helping me. I'm confused as to how the script should be written. Is it saying that whatever link that is clicked its name (x) is the $target_page so its content will be included with the template? For the homepage (no link) do I put a default as listed below--that worked when I was using switch? Can this method still let me direct all incorrect urls to my homepage as it did with switch? Can you tell me if I'm doing this right?
//content of template.php
<?php
$target_page=$_GET['x'];
$target_page=$target_page.".php";
include ($target_page);
default:
include('main_index.php');
break;
?>
Thanks so much.
Joseph
If you want your default home page:
if (isset($_GET['x'])){//tells whether or not $_GET['x'] has been set
$target_page=$_GET['x'];
$target_page=$target_page.".php";
include ($target_page);
}
else{
include('main_index.php');
}
This code will then include the main index page if someone types in http://mysite.com. If index.php has the above code main_index.php will be included.
I'm confused as to how the script should be written. Is it saying that whatever link that is clicked its name (x) is the $target_page so its content will be included with the template?
Yes you have the right idea. Just to clarify:
If you have the following link on your page
<a href="template.php?x=fishing">Fishing</a>
When you click a link, it passes x=fishing to your template.php script.
$_GET['x'] equals "fishing";
You could have
<a href="template.php?x=fishing">Fishing</a>
<a href="template.php?x=farming">Farming</a>
<a href="template.php?x=programming">Programming</a>
<a href="template.php?x=engineering">Engineering</a>
I suggest just trying out a simple php script with the code we have outlined. Nothing fancy. And see if it works for you.
Some helpful things to look up:
Query String
Hi dustbuster,
Thanks so much for taking your time to explain to me. I really appreciate it. May I ask another question? Is there a way for me to prevent someone from going to a page by typing in, for example, "http://www.mysite.com/directory/subdirectory/file.php"? When I do that I get the file.php content displayed alone instead of it being included in template.php. Just curious.
Thanks again.
Joseph
There are a number of ways you can do this:
Using the $_SERVER predefined constant you can see if the script being called is being called by another script
You can check out here to find more info.
http://php.planetmirror.com/manual/en/reserved.variables.php
Using REMOTE_ADDR and HTTP_REFFERER
I have use sessions in the past to help with such a problem.
Eg:
1) Define a session variable on template.php BEFORE you include file.php
session_start();
$_SESSION['valid_entry']='true';
2) In file.php test to see if it is set correctly
if ((isset($_SESSION['valid_entry'])) && ($_SESSION['valid_entry']=='true')){
//Do files.php content
$_SESSION['valid_entry']='false';
}
else // if $_SESSION['valid_entry'] !='true'
// OR if $_SESSION['valid_entry'] is not set
header ("Location: template.php"); //kick them back to template.php
You can add some security to pages this way as well if you have certain pages that are only meant for authorized visitors.
Hi dustbuster,
Thanks, that does make sense to me. I have installed 2 pre-written scripts: one that protects specified directories so only members can login and view the content and another one that's a forum (invisionboard). Do these scripts use similar method for authentication? I was thinking that may be there's a way to utilize that on the whole site. In other words, anyone who comes to the site will be checked 1st. If they haven't logged in every page will have a "login" button. When they do login from either the forum or the member's only directory the "login" button will become "logout". Any suggestions? Hope I'm not getting way over my head this time.
Thanks so much.
Joseph
Hi dustbuster,
I ran into a little problem. I'm getting error message (see below) on all my links (pages). I think php can't find them because they're in different directories, or subdirectories, such as: /residential/res_index.php. The sample code seems to find the page in the same directory. Please help.
Thanks.
Joseph
Warning: main(res_index.php) [function.main]: failed to create stream: No such file or directory in /home/xxxx/public_html/testing/ig/index.php on line 255
oops. Figured out that, of course, all I needed was to add the directory path to the link. For example: <a href=index.php?x="residential/res_index.php">.
Gotta get some sleep sometime soon.
No problem.
Are you still looking for help on your loggin in and out question?
I am, in fact. Any pointers? Thanks.
dustbuster - I've used your code to include pages (without using cases or if/then's), how can I implement the dynamic titles with that/
thanks, Matt
J007, I am unfamiliar with invisionboard.
However, if you have the code you can modify it to use sessions to help you if you wish.
kepster,
Are you referring to the code I originally posted?
Or are you referring to the code J007w and I have been discussing.
if just don't want user to access http://www.mysite.com/file.php instead of http://www.mysite.com/template.php?id=file
add this code in file.php at the begining
if (!eregi("template.php", $PHP_SELF)) {
die ("Don't be naughty..u can't access this file directly");
}
Here is the complete code for those want to have dynamic website..like indx.php?x=file1 ,indx.php?x=file=2....indx.php?x=file=100. I just cleaned some code and added xtra bit..
<?php
include("menu.php");
if (isset($_GET['x'])) {
$target_page=$_GET['x'];
$target_page=$target_page.".php";
if (file_exists($target_page)) {
include ($target_page);
}
else {
die ("Sorry, such file doesn't exist");
}
}
else {
include('main.php');
}
?>
<?php
if(!eregi("index.php",$PHP_SELF)) {
die("You can't access this file directly");
}
function navbar(){
echo"<center><a href=index.php>Home </a>| <a href=index.php?x=file1>page1</a>|<a href=index.php?x=file2>Page2</a></center>";
}
?>
<?php
if(!eregi("index.php",$PHP_SELF)) {
die("You can't access this file directly");
}
echo"
<html>
<head>
<title>This is title and it is page 1</title>
<head>
<body>
<center>Page 1..</center><br>";
navbar();
echo"</body>
</html>";
?>
<?php
if(!eregi("index.php",$PHP_SELF)) {
die("You can't access this file directly");
}
echo"
<html>
<head>
<title>This is title and it is page 2</title>
<head>
<body>
<center>Page 2..</center><br>";
navbar();
echo"</body>
</html>";
?>
<?php
echo"
<html>
<head>
<title>This is title and it is main page </title>
<head>
<body>
<center> This is main page/index.php</center><br>";
navbar();
echo"</body>";
echo"</html>";
?>
Well.... just play around.. this just sample. It was a good lesson for me.
Thanks guys for all the code and help.
I mean the code you originally posted in response to j007w & Jeremy - before the security stuff