Page 1 of 1
how to use links to change variables
Posted: Tue Aug 03, 2004 9:41 am
by DaRk
My site
"Nintendo Plaza" uses include to include the left column, the right, the header, and the center where the information is displayed.
I want to use the links on the left side to change the variable that controls the inclusing in the center section. How do I do that?
Posted: Tue Aug 03, 2004 9:53 am
by nigma
Say you have three nav items in the left column:
nav1, nav2, nav3
Make each link in the left column set a variable in the url like so:
You would replace index.php with the name of the main script that includes the header, nav, etc. and replace nav1 with whatever nav item it is.
Later on when you are going to include the main content do something like:
Once you get something like that setup you'll probably want to change things a bit so that the user can't ask you include a page like /etc/passwd
Ask if you need clearification.
Posted: Tue Aug 03, 2004 9:55 am
by jslick
You could put the variable in the link:
<a href="thepage.php?
page=monster">The Link</a>
Is that what you mean?
Then on the page that has all the includes:
Code: Select all
<?php
// The if statement is to check if there is a variable...if the links looks like
// http://yoursite.com/index.php
// instead of http://yoursite.com/index.php?page=monster
if ($page==null || $page=="") { @include("main.php"); }
else { @include("files/$page" . ".php"); }
// then make a page called monster.php and it includes it
?>
I used to do something similar to this (what you are trying to do) and it worked fine.
Posted: Tue Aug 03, 2004 12:51 pm
by DaRk
Yours was a bit confusing to me, Nigma. I think I'll stick with jslick's one.
Still thanks, both of you.

Posted: Tue Aug 03, 2004 1:35 pm
by feyd
note: jslick's requires register_globals to be on.. Nigma's doesn't.
Posted: Tue Aug 03, 2004 2:11 pm
by Swede78
Dark,
I just wonder why you want to use the url to pass which page gets included. Why not just create separate webpages with the correct includes hard coded into them (or the content itself)? You're going to have to make the content it's own file anyway.
Your method allows potential security risks, if you don't spiff the code up. Also, if you want search engines to give you better rankings, they prefer contentA.php more than index.php?NavID=contentA.
So instead of this setup:
Includes - Header.php, Leftnav.php, Footer.php, and $_GET['contentA']
Then in index.php you include all 4 includes, the last being based on a GET variable.
Why not just do this:
Includes - Header.php, Leftnav.php, Footer.php
Then create contentA.php that uses the appropriate includes. As for the content material, you can either use another include or even better, just put the content directly into it. That would use the same number of files, be more secure, and be more search engine-friendly.
Posted: Tue Aug 03, 2004 2:34 pm
by d3ad1ysp0rk
Taken from code
I posted on another topic:
Code: Select all
<?php
if(empty($_GET['page'])){ $page = "home"; } //default file
else{ $page = $_GET['page']; }
$safe_files = array("home","news","contact","programs");
if(!in_array($page,$safe_files)){
die("Cannot access the file you specified");
}
else {
include("some/path/to/files/".$page.".php");
}
?>
Posted: Tue Aug 03, 2004 5:06 pm
by DaRk
Swede78 wrote:Dark,
I just wonder why you want to use the url to pass which page gets included. Why not just create separate webpages with the correct includes hard coded into them (or the content itself)? You're going to have to make the content it's own file anyway.
Why not just do this:
Includes - Header.php, Leftnav.php, Footer.php
Then create contentA.php that uses the appropriate includes. As for the content material, you can either use another include or even better, just put the content directly into it. That would use the same number of files, be more secure, and be more search engine-friendly.
My site is maintained with the help of a friend and I'm willing to take the risk of search engine onfriendlyness, since my friend doesn't know php or other scripting languages I used to make the site. And I'm using the blog pivot so he can edit the contents without needing to download the whole page- edit it - and upload it again. The risk of him messing up the codes is narrowed down to a minimum.
And second I don't have much free time on my hands since my holidays end in 3 weeks and then it's back to school. So I need to have a way to edit the files fast and precise since we aren't actually allowed to use the pc's at school for self porpuses.
@feyd: Really? That must be why my old script didn't work.
I'll have a look and see which suits me best. Thanks for the help.
Edit:
I used LiLpunkSkateR's code and it worked.