Should this work, if no 'menu' is in the URL?

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
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Should this work, if no 'menu' is in the URL?

Post by simonmlewis »

Code: Select all


	<?php

function getMenu()
{
$thismenu="includes/menu/".$_GET['menu'].".inc";
include $thismenu;
}

	$menu=$_REQUEST['menu'];
	if ($menu == NULL) { $menu = "home";}
	getMenu();
	?>
If the URL is: http://www.domain.co.uk/index.php?page=home, shouldn't this pull in "home" as the menu file and place it in this section of the page?
If I set it to ".....index.php?page=home&menu=home", it works.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Should this work, if no 'menu' is in the URL?

Post by social_experiment »

simonmlewis wrote:houldn't this pull in "home" as the menu file and place it in this section of the page?
No because the script specifies $_GET['menu'] as the required value, not $_GET['page']; same for the $_REQUEST option.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Should this work, if no 'menu' is in the URL?

Post by simonmlewis »

So should I be using:
$menu=$_GET['menu']; ??
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
maxx99
Forum Contributor
Posts: 142
Joined: Mon Nov 21, 2011 3:40 am

Re: Should this work, if no 'menu' is in the URL?

Post by maxx99 »

in this url http://www.domain.co.uk/index.php?page=home you dont pass menu parameter. And getMenu() always reaches for $_GET not a parameter.

if you want to work with this you need to:

Code: Select all

 <?php

function getMenu($menu)
{
$thismenu="includes/menu/".$menu.".inc";
include $thismenu;
}

         $menu=$_REQUEST['menu'];
         if (empty($menu)) { $menu = "home";}
         getMenu($menu);
         ?>
Btw. its a terrible solution. It may seem secure with the path and file extension :)... but it isnt.
If you want to stay as simple as this, you'd better use switch/case.

Code: Select all

$menu=$_REQUEST['menu'];
switch($menu){
 case 'books':
  $include="includes/books/books.inc";
  break;
default:
  $include="includes/menu/home.inc";
  break;
}
include $include;
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Should this work, if no 'menu' is in the URL?

Post by social_experiment »

I personally wouldn't use $_REQUEST here; if you know where the value is going to come from, why not use $_GET?

Code: Select all

<?php
$menu=$_GET['menu'];  
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
maxx99
Forum Contributor
Posts: 142
Joined: Mon Nov 21, 2011 3:40 am

Re: Should this work, if no 'menu' is in the URL?

Post by maxx99 »

My bad, copy&paste fail. Agree 100% :)
Post Reply