Beginner Q: Need help linking...

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
majesticreality
Forum Newbie
Posts: 3
Joined: Sat Mar 05, 2005 2:31 pm

Beginner Q: Need help linking...

Post by majesticreality »

I'm trying to make it so that when I click say the "contact" page, the URL stays as mysite.com/index.php . I was wondering if someone could tell me how to make the complete link to do that, my friend told me to do

Code: Select all

<?php
$thefile = "$page.php";
include($thefile);
?>
But I couldn't get it to work, maybe i'm retarded? Thanks for the help!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

the most basic way to accomplish this is..

Code: Select all

include($_GET&#1111;'page'].'php');
and then you would have your.com/?page=pagename.

As you can tell pagename.php would have been executed. This is pretty bad practice though. You should generally validated any user input.

Code: Select all

$page = (empty($_GET&#1111;'page']) ? 'mainpage' : $_GET&#1111;'goto']);
This will check to see if 'page' is empty, if so assign it to 'mainpage' which is your welcome page (mainpage.php)

As for validating the field, you should generally have an array of all valid pages that you know of and check if the requested page exists and is approved

Code: Select all

$approved = array('mainpage','page423,'other','somepage');
and then check if it exists in the approved list

Code: Select all

if (in_array($page,$approved) && file_exists($page.'.php');
&#123;
     //this is where you insert the page
     include($_GET&#1111;'page'].'php');
&#125;
else
&#123;
     //include a file with an error
     include('404.html');
&#125;
majesticreality
Forum Newbie
Posts: 3
Joined: Sat Mar 05, 2005 2:31 pm

Post by majesticreality »

Thanks for the help Phenom, but if I use

Code: Select all

include($_GET&#1111;'page'].'php');
Where in the code do I put the text that i'm linking?

(who knew that adding a simple link would cause me the most trouble :( )
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post by Todd_Z »

You create files called for example

Home.php
Contact.php
News.php
Info.php

Then when you make a link like these:
<a href="www.yoursite.com/?page=Home">Home</a>
<a href="www.yoursite.com/?page=Contact">Home</a>

The value of home or contact, etc. will be stored in the variable $_GET["page"], so then when you include $_GET["page"].".php", you will include Home.php.
User avatar
Supremacy
Forum Newbie
Posts: 11
Joined: Fri Mar 04, 2005 12:54 pm
Location: Denmark

Post by Supremacy »

wouldent it be more easy just to make a frameset on top of the site??

that way the url allways says http://www.your-domain.com ?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Supremacy wrote:wouldent it be more easy just to make a frameset on top of the site??

that way the url allways says http://www.your-domain.com ?
Short answer is, no. And why would you want to URL to be domain.com all the time...
User avatar
smpdawg
Forum Contributor
Posts: 292
Joined: Thu Jan 27, 2005 3:10 pm
Location: Houston, TX
Contact:

Post by smpdawg »

You need to be very careful when using a technique like this because all someone has to do is prepend some directory information and cause some arbitrary PHP script to execute.

For example:
I have just asked your script to run a script in an entirely different directory and its effects may be devastating.

But don't let that prevent you from using this technique, which some parameter handling you can ensure that script that is being requested is valid.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Phenom wrote: and then check if it exists in the approved list

Code: Select all

if (in_array($page,$approved) && file_exists($page.'.php');
&#123;
     //this is where you insert the page
     include($_GET&#1111;'page'].'php');
&#125;
else
&#123;
     //include a file with an error
     include('404.html');
&#125;
User avatar
Supremacy
Forum Newbie
Posts: 11
Joined: Fri Mar 04, 2005 12:54 pm
Location: Denmark

Post by Supremacy »

Phenom wrote:
Supremacy wrote:wouldent it be more easy just to make a frameset on top of the site??

that way the url allways says http://www.your-domain.com ?
Short answer is, no. And why would you want to URL to be domain.com all the time...
It's REALLY nice when your working with subdomains, that way you can hide your pages. :D
User avatar
smpdawg
Forum Contributor
Posts: 292
Joined: Thu Jan 27, 2005 3:10 pm
Location: Houston, TX
Contact:

Post by smpdawg »

I just wanted to reiterate that point because many people would write it off as trivial but it isn't.
Last edited by smpdawg on Sat Mar 05, 2005 5:50 pm, edited 1 time in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

and I was trying to explain one way to prevent it, as simple as possible..

What I was trying to get across was that you should validate.
User avatar
smpdawg
Forum Contributor
Posts: 292
Joined: Thu Jan 27, 2005 3:10 pm
Location: Houston, TX
Contact:

Post by smpdawg »

It is a very valid point and I wasn't trying to minimize the importance of what you said.
Post Reply