This is my code,
## GET REQUEST POSTING TO PAGE ( /example.php?name=something )##
## Expected Output : something
$var = '$_'.strtoupper($_SERVER['REQUEST_METHOD']);// concatenating with '$_'
var_dump($var."['name']");
echo '--<br>';
var_dump($var['name']);
echo '--<br>';
print_r($var."['name']"); //not working
echo '--<br>';
print_r($var['name']); //not working
Any idea how to make this work ? what is the correct way ?
Use double $
$var = "_" . $_SERVER['REQUEST_METHOD'];
var_dump($$var);
But this is "dirty hack"
For good code use if or switch for init variable.
switch($_SERVER['REQUEST_METHOD']) {
case "POST":
$var = $_POST;
break;
case "GET":
default:
$var = $_GET;
break;
}
Related
I am trying to display content depending on page id, however when I add nothing to the url, so just index.php I get an error saying that $p is not defined, how can I give this var a default value that's outside the switch case?
<?php
$p = $_GET['p'];
switch ($p) {
case 1:
$content1 = new login();
$content = $content1->displayLogin();
break;
case 2:
echo "ID is 2";
break;
case 3:
echo "ID is 3";
break;
default:
$content1 = new dbconnection();
$content = $content1->displayTable();
}
I understand that you want to make $p have a default value if $_GET['p'] is not defined.
You can do it like this:
$p = isset($_GET['p']) ? $_GET['p'] : 'defaultValue';
or, if you're on PHP 7:
$p = $_GET['p'] ?? 'defaultValue';
Replace:
$p = $_GET['p'];
With:
$p = !empty($_GET['p']) ? $_GET['p'] : default_id_value_here;
You can do the following:
$p = $_GET['p'] ?? <default value>; // e.g 1 or 3 etc.
Read more about it at this question: PHP syntax question: What does the question mark and colon mean?
here's some pseudo-code (it's not written correctly, the point of my ? is the variable, not the switch):
switch ($action) {
case "1":
//this is a function
case "2":
//this is a function
//etc.
}
How should this be written:
$variable = result of function in case 1.
Your switch statement is wrong . It requires a break keyword between every case
$action = 1;
$result = "Success";
switch ($action) {
case 1:
$variable = $result;
echo $variable;//prints Success
//this is a function
break; // like this
case 2:
//this is a function
break;//
//etc.
}
Just run the function(s) (you can pass args in too) as part of the code within the case / break blocks like this :
$action = 1;
switch ($action) {
case 1:
$variable = someFunctionOne();
break;
case 2:
$variable = someOtherFunctionTwo();
break;
//etc.
}
how to variable the result of php switch.
ex:
<?php
//variables of cases
$var_1 = 1;
$var_2 = 2;
$var_3 = 3;
$var_0 = 0;
//end variables of cases
//action variable
$action = 10;
//end action variable
//start switch
switch ($action) {
case "1":
echo "$var_1;";
break;
case "2":
echo "$var_2;";
break;
case "3":
echo "$var_3;";
break;
default:
echo "$var_0;";
}
//receives the value of the switch.
$switch_result = get_result_case;
//in this my example I need to enter the value of the case in a variable.
?>
in this my example I need to enter the value of the case in a variable.
I have this PHP switch:
<?php
$destination = isset($_GET['act']);
switch ($destination) {
default:
echo "test";
break;
case "manage":
echo "manage things";
break;
case "create":
echo "create things";
break;
}
?>
However, when I go to test.php?act=create, the output is manage things not create things.... and when I go to test.php?act=manage -- of course I get manage things...
So ... how do I fix this? Thank you
php's isset returns a boolean. So $destination is either true or false, not a string.
Try
if(isset($_GET['act']))
$destination = $_GET['act'];
Your problem is:
$destination = isset($_GET['act']);
isset returns either true or false, never any of the string values you are using.
You could use something like:
$destination = isset($_GET['act']) ? $_GET['act'] : '';
You have to use:
<?php
if(isset($_GET['act'])) $destination = $_GET['act'];
switch ($destination) {
case "manage":
echo "manage things";
break;
case "create":
echo "create things";
break;
default:
echo "test";
}
Or just use:
$destination = #$_GET['act'];
I am unable to set session for $_SESSION['next'] under switch/case condition, while $_SESSION['user_id'] works perfectly before the condition. The script run into each condition of switch/case condition and redirect without setting $_SESSION['next']. Is there any specific reason why it fails to work? How to solve this?
require_once ('../src/facebook.php');
require_once ('../src/fbconfig.php');
//Facebook Authentication part
$user_id = $facebook->getUser();
if ($user_id <> '0' && $user_id <> '') {
session_start();
$_SESSION['user_id'] = $user_id;
switch((isset($_GET['page']) ? $_GET['page'] : '')){
case 'abc';{
$_SESSION['next'] = 'AAA';
echo "<script>top.location.href = 'https://www.example.com/xxx/'</script>";
exit;}
case 'def';{
$_SESSION['next'] = 'BBB';
echo "<script>top.location.href = 'https://www.example.com/xxx/'</script>";
exit;}
case 'ghi';{
$_SESSION['next'] = 'CCC';
echo "<script>top.location.href = 'https://www.example.com/xxx/'</script>";
exit;}
default;{
echo "<script>top.location.href = 'https://www.example.com/xxx/'</script>";
exit;}
}
} else {
echo "<script>top.location.href = 'https://www.example.com/xxx/'</script>";
exit;
}
Your switch is all wrong. Read the manual and try this:
<?php
switch ((isset($_GET['page']) ? $_GET['page'] : '')){
case 'abc':
$_SESSION['next'] = 'AAA';
echo "<script>top.location.href = 'https://www.example.com/xxx/'</script>";
break;
case 'def':
$_SESSION['next'] = 'BBB';
echo "<script>top.location.href = 'https://www.example.com/xxx/'</script>";
break;
case 'ghi':
$_SESSION['next'] = 'CCC';
echo "<script>top.location.href = 'https://www.example.com/xxx/'</script>";
break;
default:
echo "<script>top.location.href = 'https://www.example.com/xxx/'</script>";
break;
}
You're using exit in your switch, which (unless you want your script to end at the switch) is a no-no. Instead, you have to use the break keyword.
You also use semicolons and curly braces for each case.
case 'ghi';{ ... }
NO! Proper usage is
case 'ghi':
.
.
.
break;
Update: I just noticed you use this line:
if ($user_id <> '0' && $user_id <> '') { ... }
What is <> doing in PHP code? The "standard" operator for "not equals" is != in PHP. Use it correctly or no one will want to use your code.
Second update: You never set $_SESSION['next'] in your default case. It's very likely that your switch is always going to the default case. This would cause the behavior you're experiencing.
I suggest:
if (($user_id != '0') && ($user_id != ''))
(parentheses, and the != operator)
and also a DRYer switch:
$page = array_key_exists('page', $_GET) ? $_GET['page'] : '';
switch ($page) {
case 'abc':
$next = 'AAA';
$loc = 'https://www.example.com/xxx/';
break;
case 'def':
$next = 'BBB';
$loc = 'https://www.example.com/yyy/';
break;
... // and so on
}
if (isset($next)) {
$_SESSION['next'] = $next;
// If it does not work, you have problems with your session ID, maybe?
}
// I find this syntax easier
print <<<SCRIPT
<script type="text/javascript">
top.location.href = '$loc';
</script>
SCRIPT;
exit();
I have a html form with text input field. I'm wondering how to recognize specific input from the form. Example input commands:
<input type="text" name="action" value="bookmark http://google.com" />
<?php
if ($command == "goto"):
// go to website X
elseif ($command == "bookmark"):
// bookmark website X
else:
// something else
endif;
?>
I think the easiest way is to split the string on the first space to separate it into a command and the parameter for that command. The "2" parameter to explode() allows spaces to be used in $param, if necessary.
$input = explode(' ', $_POST['action'], 2);
$command = $input[0];
$param = $input[1];
switch ($command) {
case 'goto':
// go to website $param
break;
case 'bookmark':
// bookmark website $param
break;
default:
// unknown command
}
$aAct = explode(' ', $_POST['action');
if(is_array($aAct)) {
switch($aAct[0]) {
case 'bookmark':
/* do action e.g. header('Location: ' . $aAct[1]); */
break;
}
}
Make a case/break combination for every action you intend to specify..
Something like this?:
//get the command from your value
$command = current(explode(" ", $_POST['action']));
//get the url from your value
$url = next(explode(" ", $_POST['action']));
And as stated by karim79, a switch to handle the input would more appropriate.
switch($command) {
case 'goto':
// do stuff with $url;
break;
case 'bookmark':
// do stuff with $url;
break;
default: // do something default;
}
hope it helps
Try this:
$request = $_POST['action'];
$split = explode(' ',$request,2);
$command = $split[0];
if(!isset($split[1])){
//no url
die;
}
$url = $split[1];
if($command == "goto"){
header('location: '.$url);
die;
}elseif($command == "bookmark"){
header('location: '.$url);
die;
}else{
echo 'No Commands :(';
}
Use $_POST or $_GET to retrive the request data. ie: $_GET['action']
Set the header location to redirect the browser. die; or exit; is used to terminate and output the current script