how to use links to change variables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
DaRk
Forum Newbie
Posts: 4
Joined: Tue Aug 03, 2004 9:41 am

how to use links to change variables

Post 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?
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post 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:

Code: Select all

index.php?pagetoshow=nav1
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:

Code: Select all

include($_GET['pagetoshow']);
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.
jslick
Forum Commoner
Posts: 35
Joined: Wed Jul 21, 2004 11:18 am

Post 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.
DaRk
Forum Newbie
Posts: 4
Joined: Tue Aug 03, 2004 9:41 am

Post by DaRk »

Yours was a bit confusing to me, Nigma. I think I'll stick with jslick's one.
Still thanks, both of you. :D
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

note: jslick's requires register_globals to be on.. Nigma's doesn't.
Swede78
Forum Contributor
Posts: 198
Joined: Wed Mar 12, 2003 12:52 pm
Location: IL

Post 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.
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post 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");
} 
?>
DaRk
Forum Newbie
Posts: 4
Joined: Tue Aug 03, 2004 9:41 am

Post 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.
Post Reply