Jump to content

Recommended Posts

hi i recently tried using the switch method for a navigation system as opposed to using the get method.

this is the code i am using with the get method commented out:

<?php // Opening tag for PHP

/*$p = $_GET['p'];
if ( !empty($p) && file_exists('./pages/' . $p . '.php') && stristr( $p, '.' ) == False ) 
{
   $file = './pages/' . $p . '.php';
}
else
{
   $file = './pages/default.php';
}

include $file;*/

switch(£_GET['p']){
case "contact":
//sets the variable to contact
$file = './pages/contact.php';
break;

case "portfolio"
$file = './pages/portfolio.php';
break;


}

include $file;

?>

 

this is the code for my index file.

if the url is blank there is no file to include. however i do already have a file that i want to include.

how would i check to see if the url is blank using switch so i can include this file?

 

 

and also. when using switch do i still put ?=variable as the link for links?

and is it supposed to seen in the url like: www.domain.com/index.php?=variable  ?????

Link to comment
https://forums.phpfreaks.com/topic/50817-solved-help-switch-statement/
Share on other sites

switch(£_GET['p']){
  case "contact":
    //sets the variable to contact
    $file = './pages/contact.php';
    break;
  case "portfolio"
    $file = './pages/portfolio.php';
    break;
  default:
    $file = "./pages/yourdefault.php";
    break;
}

include $file;

?>

In a switch statement, after all your case statements, just set up a default at the end to catch anything besides the cases you have declared:

<?php
switch ($_GET['pg']) {
case 'contact':
	require('contact.php');
	break;

case 'about':
	require('about.php');
	break;

default:
	require('default.php');
}
?>

 

Hope this helps.

Hey dude

 

I tend to use this

 

 

$view = $_GET['view'];

// Navigation

if (!empty($view) && file_exists("./" . $view . ".php"))
{
$file = "./" . $view . ".php";
}
elseif (empty($view))
{
$file = "main.php";
}
else
{
$file = "not_exist.php";
}

include $file;

 

I think this would do a similar function to what you are after, plus if the page doesnt exist it redirects to a custom 404 page.

mort thanks neways but ive been told to steer away from the get method as it is unsafe i suggest you take a leaf out of this book.. maybe?

 

Yes. using the $_GET variable directly is unsafe. mort, if your server has url_wrappers enabled your code is open to script injection.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.