php get id from url at default - php

Is this correct or not because, I feel like it is wrong.
Image to the error via wamp
link to the site
http://rank.hellforge.org
<?php
$id = $_GET['id'];
switch($id)
{
case "1":
#include("servers/server1.php");
break;
// Uncomment below if you are gonna have multuple servers, and copy paste fore more
case "2":
#include("servers/server2.php");
break;
default:
#include("servers/server1.php");
}
?>

if you want to retrieve a parameter in the url this parameter should be passed. And this is done with the symbol '?' followed by the name of the parameterhttps://exemple?param=valeur
and you can retrieve it with $_GET['param']

A better way to implement this, as mentioned by comments, you have to check the $_GET if it got an id element, also for the switch case remove the check for case 1 and have it as default for readability and reducing code complexity.
<?php
$id = (!empty($_GET['id'])) ? $_GET['id'] : '';
switch($id) {
case "2":
#include("servers/server2.php");
break;
default:
#include("servers/server1.php");
}
?>

Related

Issue with switch statement and $_GET

I have an issue with this switch case, I will be glad if someone could give me a hint on what's going wrong.
The lines below keep on adding the case 1, 2 or 3 below the default one (centre_1) instead of replacing it.
I can't find where it goes wrong. Here is the code :
<?php include("mx/centre_1.php");
if(!isset($_GET['page'])) {
$page='centre_mx.php';
} else {
switch($_GET['page']) {
case'centre_1':
include('/mx/centre_1.php');
break;
case'centre_2':
include('/mx/centre_2.php');
break;
case'centre_3':
include('/mx/centre_3.php');
break;
}
}
?>
Any assistance will be helpful.
The reason '/mx/centre_1.php' is always being displayed is because you have it at the top of your code, outside of the if and switch statement. Due to this it'll be included on every page load.
In order to only have '/mx/centre_1.php' appear when no other option is selected you need to make it the default switch case.
$page = '';
if(in_array('page', $_GET)) {
$page = $_GET['page'];
}
switch($page) {
case 'centre_2':
include('/mx/centre_2.php');
break;
case 'centre_3':
include('/mx/centre_3.php');
break;
default:
include('/mx/centre_1.php');
}
The default case in a switch statement will happen when none of the other cases match the variable provided. That is, if $_GET['page'] doesn't equal centre_2 or centre_3 then the default code will be performed.
I'd suggest reading up more on switch statements here, since you don't seem to understand how they work.

I use switch statement to include specific file but it includes the "default" also

Here's my code
<?php
$id = isset($_GET['id']) ? $_GET['id'] : '';
switch ($id) {
default:include_once('default.php');
break;
case 'actiune':include('jocuri/actiune/index.php');
break;
case 'aventura':include('jocuri/aventura/index.php');
break;
}
?>
<!--games-->
<?php
$game = isset($_GET['game']) ? $_GET['game'] : '';
switch ($game) {
case 'nz':include('jocuri/actiune/ninja-vs-zombie/index.php');
break;
case 'aventura':include('/jocuri/aventura/index.php');
break;
}
?>
So when I try to acces case 'nz' from it also includes default from the top part. So how can i do to include only the 'nz' case?
Try moving the default statement to the bottom of the first switch statement. A switch statement goes straight down each case path, hence the need for the break keyword. The default case will always execute in the case no matter what. Putting it at the bottom will ensure it will only get executed when the cases fail.
As I understand you would like to ignore 'top part' (including file by id) when game parameter is provided.
Try to add IF statement to execute first switch-statement only if game is not provided (or not matched with your provided cases ['aventura', 'nz'])
Moreover you can do something like that:
$id = isset($_GET['id']) ? $_GET['id'] : '';
$game = isset($_GET['game']) ? $_GET['game'] : '';
$games = array('nz' => 'jocuri/actiune/ninja-vs-zombie/index.php',
'aventura' => '/jocuri/aventura/index.php');
$ids = array('actiune' => 'jocuri/actiune/ninja-vs-zombie/index.php',
'aventura' => '/jocuri/aventura/index.php');
if (array_key_exists($game, $games))
{
include($games[$game]);
}
else if (array_key_exists($id, $ids))
{
include($ids[$id]);
}
else include('default.php');
try this
<?php
$id = isset($_GET['id']) ? $_GET['id'] : '';
switch ($id) {
case 'actiune':include('jocuri/actiune/index.php');
break;
case 'aventura':include('jocuri/aventura/index.php');
break;
default:include_once('default.php');
break;
}
?>
<!--games-->
<?php
$game = isset($_GET['game']) ? $_GET['game'] : '';
switch ($game) {
case 'nz':include('jocuri/actiune/ninja-vs-zombie/index.php');
break;
case 'aventura':include('/jocuri/aventura/index.php');
break;
}
?>
(I did try to add this as a comment to osulerhia's answer but couldn't and figured it might answer the posters question if I've read it correctly).
I'm not sure if I understand your question correctly and if osulerhia's answer above is what you're looking for.
You're expecting two separate GET variables ("id" and "game") and if the game variable is "nz" you don't want to show the default from the switch statement you're using for "id"?
If that's right then you need to add some sort of logic to how you want your switch statements to play out. You then also need to consider though whether you want either of the others to show if "nz" is the value of "game". Your logic (written plainly) is currently:
Is the id variable "actiune" -> include a file
Is the id variable "aventura" -> include a file
Is the id variable anything else -> include the default file
Is the game variable "nz" -> include a file
Is the game variable "aventura" -> include a file
As you can see both of your switches are completely separate, you need to decide what you want to display and when you want to display it, for example, if you wanted everything to work as above but to not display ANYTHING from the first switch if the value of the game variable is "nz" then you could wrap it in an if statement like:
if((isset($_GET['game']) && $_GET['game'] != "nz") || (!isset($_GET['game']))) { *your original switch statement* }

PHP Switch Case Url's

We currently use Switch case url config to help us with the navigation on some of our urls, Im not sure if there is an easier way to do it but i couldnt seem to find 1.
<?php if (! isset($_GET['step']))
{
include('./step1.php');
} else {
$page = $_GET['step'];
switch($page)
{
case '1':
include('./step1.php');
break;
case '2':
include('./step2.php');
break;
}
}
?>
Now this system works perfectly but the only snag we hit is if they type in xxxxxx.php?step=3 boom they just get a blank page and that should be correct as there is no case for it to handle '3' but what i was wondering is .. is there any php code i could add to the bottom that may tell it for any case other than those 2 to redirect it back to xxxxx.php ?
Thanks
Daniel
Use the default case. That is, change your switch to something like this:
<?php if (! isset($_GET['step']))
{
include('./step1.php');
} else {
$page = $_GET['step'];
switch($page)
{
case '1':
include('./step1.php');
break;
case '2':
include('./step2.php');
break;
default:
// Default action
break;
}
}
?>
The default case will be executed for every case which is not explicitly specified.
All switch statements allow a default case that will fire if no other case does. Something like...
switch ($foo)
{
case 1:
break;
...
default:
header("Location: someOtherUrl");
}
would work. You may, however, want to Google around for other, more robust and extensible, page dispatch solutions.
How about a different approach with something along the lines of:
<?php
$currentStep = $_GET['step'];
$includePage = './step'.$currentStep.'.php'; # Assuming the pages are structured the same, i.e. stepN where N is a number
if(!file_exists($includePage) || !isset($currentStep)){ # If file doesn't exist, then set the default page
$includePage = 'default.php'; # Should reflect the desired default page for steps not matching 1 or 2
}
include($includePage);
?>

how do i add two values for one case using switch?

im trying to create a website using switch commands to navigate with the contents displayed in a table via echo content. everything works fine. but one of the pages consists of multiple pages. The address looks like this website/index.php?content=home etc.
but i want to make it like this for the page consisting multiple pages website/index.php?content=home&page=1
my index code:
<?php
switch($_GET['content'])
{
case "home":
$content = file_get_contents('home.php');
$pages = file_get_contents('pages/1.php'); //like this?
break;
case "faq":
$content = file_get_contents('faq.php');
break;
default:
$content = file_get_contents('home.php');
break;
}
?>
//some code
<?php echo $content; ?>
//some code
the home php:
<?php
switch($_GET['page'])
{
default:
case "1":
$page = file_get_contents('pages/1.php');
break;
default:
$page = file_get_contents('pages/1.php');
break;
}
?>
// some code
<?php echo $page; ?>
//some code
go to page etc.
but when i do it like this the echo command shows me the code of the home.php but not the one of the page i wanna load inside of it.
i appreciate any kind of help!
"default" must logically always come last in your switch, since it will match any input that hasn't already been matched by a previous "case" statement.
If you want to do certain actions for multiple values, you can do it like this:
switch ( $var ) {
case 'foo':
case 'bar':
// Do something when $var is either 'foo' or 'bar'
break;
default:
// Do something when $var is anything other than 'foo' or 'bar'
break;
}
Try using include() instead.
Also, as #Rob said, your second switch() statement is ill formatted. default: should always be last and is used as a "catch-all" for values you didn't specify earlier.
first of all, why don't you use include() instead of file_get_contents()?
Second of all: you could use your "manager" page in this way:
<?php
$myPage = $_GET['page'];
$myContent = $_GET['content']
switch($myContent){
case "mycontent":
case "home": include('/home.php');
if(!empty($myPage)){
include ('/pages/'.$myPage.'.php');
}
break;
default:
// do whatever you want
}
?>
Be careful of (as in steweb's example) including files based on user input, even with a prepended path — remember that there's the possibility of executing untrusted code. (Imagine if $_GET['page'] is set to ../../../some-evil-file.php, for example.)
To avoid this, it's easiest to use a whitelist:
$pages = array( 1, 2, 18, 88 );
switch ( $_GET['content'] ) {
case 'home':
if ( in_array( $_GET['page'], $pages ) ) {
include 'pages/' . $_GET['page'] . '.php';
} else {
// If an invalid page is given, or no page is
// given at all, include a default page.
include 'pages/1.php';
}
break;
}

Finding part of a string that a user has sent via POST

My users can send links from popular file hosts like Rapidshare, Megaupload, Hotfile and FileFactory. I need to somehow find out what filehost they sent the link from and use the correct class for it appropriately.
For example, if I sent a Rapidshare link in a form on my web page, I need to somehow cycle through each file host that I allow until I find the text rapidshare.com, then I know the user has posted a Rapidshare link.
Perhaps a PHP example:
switch($_POST['link'])
{
case strstr($_POST['link'], 'rapidshare.com'):
// the link is a Rapidshare one
break;
case strstr($_POST['link'], 'megaupload.com'):
// the link is a Megaupload one
break;
case strstr($_POST['link'], 'hotfile.com'):
// the link is a Hotfile one
break;
case strstr($_POST['link'], 'filefactory.com'):
// the link is a Filefactory one
break;
}
However, I know for a fact this isn't correct and I'd rather not use a huge IF statement if I can help it.
Does anyone have any solution to this problem?
If you need me to explain more I can try, English isn't my native language so it's kinda hard.
Thanks all.
Nevermind guys, I used this:
$sentLink = trim($_POST['link']);
$host = parse_url($sentLink, PHP_URL_HOST);
switch($host)
{
case 'rapidshare.com':
echo "RS";
break;
case 'megaupload.com':
echo "MU";
break;
case 'hotfile.com':
echo "HF";
break;
case 'filefactory.com':
echo "FF";
break;
default:
echo "WTF! D:";
}
First time I've heard of parse_url :)
According to php - regex hostname extraction you want to make sure you check for the www. portion of th eurl as parse_url can sometimes return that as well.
$sentLink = trim($_POST['link']);
$host = array_shift( explode( '.', str_replace('www.', '', parse_url( $sentLink , PHP_URL_HOST )) ) );
switch($host)
{
case 'rapidshare.com':
echo "RS";
break;
case 'megaupload.com':
echo "MU";
break;
case 'hotfile.com':
echo "HF";
break;
case 'filefactory.com':
echo "FF";
break;
default:
echo "WTF! D:";
}

Categories