Query string navigation

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
187skillz
Forum Commoner
Posts: 39
Joined: Sun Aug 10, 2003 8:35 pm
Location: London

Query string navigation

Post by 187skillz »

Hi..
I'm new to php, I read the phpcomplete beginner's tutorial and it made so much sense, so big up to the Author.
I'm building my site using php and I was wondering how to do a proper query string navigation..
I read some other tutorial that said:

Change your link too

Code: Select all

<a href="?section=about">about</a>

Create an "about.php" page
And then:

Code: Select all

<?php
<?php
if($section=="about") include("about.php") ;
elseif($section=="music") include("music.php") ; 
?>
?>
OK I'm a lil confused, where does the php (the second one)code go? does it go to the "about.php" page?
I'm new, just getting my feet wet with php, I read some tuts but I'm reading it now/building my site at the same time, sorry if this question is dumb, getting it has been hard until phpcomplete tutorial.

Thanks!
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Code: Select all

// in the folders, where the link is clicked, index page 
<a href="?section=about">about</a>
// same as
<a href="index.php?section=about">about</a>

// in about.php
<a href="about.php?section=about">about</a>
You should also try the last link in my signature. Jason has made some pretty good examples there. (Regarding _POST and _GET)
qartis
Forum Contributor
Posts: 271
Joined: Sat Dec 14, 2002 4:43 pm
Location: BC, Canada
Contact:

Post by qartis »

But to answer your first question, yes, the "about page" code would be put in about.php.
toms100
Forum Contributor
Posts: 119
Joined: Wed Feb 26, 2003 10:29 am
Location: Bristol,UK

Post by toms100 »

just incase register_globals is off (should be)

Code: Select all

<?php
if($_GET['section']=="about") include("about.php") ; 
elseif($_GET['section']=="music") include("music.php") ; 
?>
187skillz
Forum Commoner
Posts: 39
Joined: Sun Aug 10, 2003 8:35 pm
Location: London

Post by 187skillz »

Ok thanks to everyone that's answered, pls take a look at this online version
Beta
why is it that when I click on "more" it leads back to the home.php instead of about.php?

thanks for your time!
qartis
Forum Contributor
Posts: 271
Joined: Sat Dec 14, 2002 4:43 pm
Location: BC, Canada
Contact:

Post by qartis »

Because the HTML link is leading to home.php?section=about , not about.php
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

This:

home.php?section=about

.. runs the home.php script and declares

Code: Select all

<?php
$section = 'about'; 
?>
.. in the global scope of the script.

As mentioned above, this will only work with register globals on. A var defined in a url is a $_GET var: register globals on will automatically declare these in the global scope of the script (along with $_POST, $_SESSION etc). That can be a security risk. With register globals off, you get the $section var from the $_GET superglobal array as: $_GET['section'].

If you want that to open a new page ("About My Site"?) then, according to the code you posted, home.php would need to if/else on $section (or rather $_GET['section']), including the appropriate file to build the page - which it doesn't at present.

It's better if your home page is in an "index.php" file rather than "home.php": http://www.yoursite.com/index.php will always be called by default simply by typing in the "http://www.yoursite.com" domain - but other filenames won't.
Last edited by McGruff on Wed Aug 10, 2005 10:49 pm, edited 1 time in total.
187skillz
Forum Commoner
Posts: 39
Joined: Sun Aug 10, 2003 8:35 pm
Location: London

Post by 187skillz »

Thanks Mcgruff, appreciate your time!
qartis
Forum Contributor
Posts: 271
Joined: Sat Dec 14, 2002 4:43 pm
Location: BC, Canada
Contact:

Post by qartis »

It's better if your home page is in an "index.php" file rather than "home.php": http://www.yoursite.com/index.php will always be called by default simply by typing in the "http://www.yoursite.com" domain - but other filenames won't.
That all depends on how you've set up Apache--DirectoryIndex for the specific directory could very well be set to the file 'I_am.great', which would even be parsed by php :)

It's all in the server. To follow conventional naming convention, index.php is the standard, though I've seen some pretty wacky extensions (.sex, .file, .gay, .dot, .php.php, .asdf, et al.)
Post Reply