Send value to php file - php

I'm try to send a value to a PHP file, but when I check, this value became null.
I send the value by: user_login.php?p_action=New_User
The code of user_login.php is:
require("include/session_inc.php");
require("include/user_handling_inc.php");
require("include/db_inc.php");
start_Session(false, false);
switch ($p_action) {
case 'Login': {
$l_flag = verify_User($p_in_username, $p_in_password);
if ($l_flag == "Not_Found") {
$l_flag = "New_User";
}
break;
}
case 'Save_Profile': {
$l_flag = "Save_Profile";
break;
}
case 'New_User':
$l_flag = "New_User";
break;
case 'Create_New_User':
$l_flag = "Create_New_User";
}
switch ($l_flag) {
case 'New_User': {
include "include/user_new_inc.php";
break;
}
case 'Save_Profile': {
load_User_Data(" username = '$p_in_username' ", false);
include "include/user_profile_save_inc.php";
break;
}
case 'Wrong_Password':
echo "Wrong Pass";
break;
case 'OK':
load_User_Data(" username = '$p_in_username' ", true);
store_User_Cookie($g_userdata->user_id);
include "include/user_profile_inc.php";
break;
case 'Create_New_User':
$l_user_id = create_New_User ($p_in_username, $p_in_email, 'Y');
if ($l_user_id != -1) {
store_User_Cookie($l_user_id);
echo "Success !! <br><br> \n";
echo "<a href\"/index.php\"> Back to Main </a>";
}
break;
}

First your code isn't correct please read more about using Switch here
second to access to any variable came from url you can use Global variable $_GET or $_REQUEST
and you can read more about them from here and here
and this is your code after fixing it please try to run it
<?php
require("include/session_inc.php");
require("include/user_handling_inc.php");
require("include/db_inc.php");
start_Session(false, false);
$p_action=$_GET["p_action"];
switch ($p_action) {
case 'Login':
$l_flag = verify_User($p_in_username, $p_in_password);
if ($l_flag == "Not_Found") {
$l_flag = "New_User";
}
break;
case 'Save_Profile':
$l_flag = "Save_Profile";
break;
case 'New_User':
$l_flag = "New_User";
break;
case 'Create_New_User':
$l_flag = "Create_New_User";
break;
}
switch ($l_flag) {
case 'New_User':
include "include/user_new_inc.php";
break;
case 'Save_Profile':
load_User_Data(" username = '$p_in_username' ", false);
include "include/user_profile_save_inc.php";
break;
case 'Wrong_Password':
echo "Wrong Pass";
break;
case 'OK':
load_User_Data(" username = '$p_in_username' ", true);
store_User_Cookie($g_userdata->user_id);
include "include/user_profile_inc.php";
break;
case 'Create_New_User':
$l_user_id = create_New_User ($p_in_username, $p_in_email, 'Y');
if ($l_user_id != -1) {
store_User_Cookie($l_user_id);
echo "Success !! <br><br> \n";
echo "<a href\"/index.php\"> Back to Main </a>";
}
break;
}
?>

you need to make the code like this friend
switch ($_GET["p_action"]) {
case 'Login': {
$l_flag = verify_User($p_in_username, $p_in_password);
if ($l_flag == "Not_Found") {
$l_flag = "New_User";
}
that well give you the value of the get!!!

Use $_GET to get your parameter.
Sometimes $_REQUEST is preferable since it access both get & post data.
2nd thing never trust the user input so you must use addslashes(); or real_escape_string() function to prevent attacks on the system.
So Code would be like this :
$var = addslashes($_GET['p_action']);
switch($p) {
case 'Login':
$l_flag = verify_User($p_in_username, $p_in_password);
if ($l_flag == "Not_Found") {
$l_flag = "New_User";
}
break;
"OTHER CASES HERE"
}
Notice that : Don't add { } for CASE. Read syntax for switch
here.

Related

How can I pass this variable and check if it matches?

How can I correct/simplify this and put it in an array?
A link is passing: somelink.php?w=a (or b,c,d)
I want the page (somelink.php) to determine if "w" is set, and if set and the var matches, include the specified page.
<?php
if(isset($_GET['w'])&&($GET['w'] == "a")){include("1.htm");}
if(isset($_GET['w'])&&($GET['w'] == "b")){include("2.htm");}
if(isset($_GET['w'])&&($GET['w'] == "c")){include("3.htm");}
if(isset($_GET['w'])&&($GET['w'] == "d")){include("4.htm");}
else{include("1.htm");}
?>
try using:
$w = $_GET['w'];
if(isset($w)) {
switch(strtolower($w)) {
case "a":
include("1.htm");
break;
case "b":
include("2.htm");
break;
case "c":
include("3.htm");
break;
case "d":
include("4.htm");
break;
default:
include("not-found.htm");
break;
}
}
Use a switch statement:
if(isset($_GET['w']))
{
switch($_GET['w'])
{
case 'a': include("1.html"); break;
case 'b': include("2.html"); break;
case 'c': include("3.html"); break;
case 'd': include("4.html"); break;
default: include("1.html"); break;
}
} else {
include("1.html");
}
how about a simple array
$x=array('a'=>'1.html','b'=>'2.html');
then
include $x[$GET['w']];
Like this:
if(isset($_GET['w'])){
switch($_GET['w']){
case "a":
include("1.htm");
break;
case "b":
include("2.htm");
break;
case "c":
include("3.htm");
break;
case "d":
include("4.htm");
break;
}
}
But I wouldn't do it that way. I'd make it so that the name of the page corresponds to the value being retrieved from the $_GET variable. That way you could do something like this.
if(!empty($_GET['w'])){
include($_GET['w'] . ".htm");
}
Of course, you'd want a little filtering of the $_GET var too to make sure it doesn't get something you don't want there. Maybe like this.
$acceptable_values = array("some","acceptable","values");
if(!empty($_GET['w']) && in_array($_GET['w'],$acceptable_values) ){
include($_GET['w'] . ".htm");
}
As I'm sure you are aware, passing variables directly into include statements or database queries is a TERRIBLE idea. See here for why in this case.
http://websec.wordpress.com/2010/02/22/exploiting-php-file-inclusion-overview/
You could do a few things, lets take a look at some of them.
<?php
$webpage = '';
if(isset($_GET['w']))
$webpage = strtolower($_GET['w']);
switch($webpage)
{
case 'b':
include '2.html';
break;
case 'c':
include '3.html';
break;
case 'd':
include '4.html';
break;
default:
include '1.html';
break;
}
Or we could use arrays
<?php
$webpage = '';
if(isset($_GET['w']))
$webpage = strtolower($_GET['w']);
$included_pages = array(
'a' => '1.htm',
'b' => '2.htm',
'c' => '3.htm',
'd' => '4.htm',
);
// Check inside our array
if(array_key_exists($webpage, $includes))
{
include $included_pages[$webpage];
}
else
{
// Couldn't find the site
include '1.htm';
}

Roll to another case in PHP switch

For example I have this:
switch ($action)
{
case 'my_action':
doSomething();
break;
case 'second_action':
if ($this_is_true)
{
$action = 'my_action';
}
else
{
doSomethingElse();
}
break;
}
Is the example above going to go through the switch again and then call the first case my_action if the second case second_action has $this_is_true variable set to true?
If this doesn't work, what would be an alternative?
You can try something like this:
switch ($action)
{
case 'my_action':
case 'second_action':
if ($this_is_true || $action=='my_action')
{
doSomething();
}
else
{
doSomethingElse();
}
break;
}
When $action is equal to 'my_action' it will run through the case, as it finds no break sentence then it will run through the second case until it finds the break sentence.
Please give a look to example #3 in http://php.net/manual/en/control-structures.switch.php to find out more about no breaking switch cases.
No it won't. Just call doSomething(); in the 'second_action' case.
I wouldn't use a switch in this simple case, but if it is very long then maybe:
$do = false;
switch ($action)
{
case 'my_action':
$do = true;
break;
case 'second_action':
if ($this_is_true)
{
$action = 'my_action';
$do = true;
}
else
{
doSomethingElse();
}
break;
}
if($do) { doSomething(); }
Try to do it this way:
do {
switch ($action)
{
case 'my_action':
$this_is_true = false;
doSomething();
break;
case 'second_action':
if ($this_is_true)
{
$action = 'my_action';
}
else
{
doSomethingElse();
}
break;
}
} while($this_is_true);
Do not forget to switch $this_is_true to false where it needed!
But this code is not beautiful... May be you better to refactor your code.

PHP Switch using it with Functions

I have this switch and I do certain things based on the selection of the action with the switch. Based on my testing, function thats in the switch is not even taking place when the page runs.
I am interested in being able to running the sortby action for now. when I go to the page, switch puts me in the first case but does not run the function.Why? How do I fix it?
switch ($_GET['action']) {
case 'sortby':
sort_by($_GET['sortby']);
break;
case 'add':
resident_add($_GET['residentID']);
include('inc/modify/add.php');
break;
case 'edit':
resident_edit($_GET['residentID']);
include('inc/modify/edit.php');
break;
case 'delete':
resident_delete($_GET['residentID']);
include('inc/modify/delete.php');
break;
case 'search':
echo "";
break;
default:
resident_default($_GET['sortby']);
}
function sort_by($sortby) {
if ($sortby == "last_name") {
$sort_db_field = "Last Name";
$sort_order = "ASC";
} elseif ($sortby == "lot") {
$sort_db_field = "Lot";
$sort_order = "ASC";
} elseif ($sortby == "date_added") {
$sort_db_field = "No";
$sort_order = "DESC";
} else {
include('inc/error.php?error_code=100');
}
return $sort_db_field;
return $sort_order;
}
$data = mysqli_query($dbcon, "SELECT * FROM `residents` ORDER BY `residents`.`".$sort_db_field."` ".$sort_order."") or die(mysqli_error());
You can't have 2 return statements in a function, the second one will never be executed.
Declare two variable before your switch:
$sort_db_field = "";
$sort_order = "";
switch ($_GET['action']) {
/*snip*/
}
Then in the function drop the returns. This function will now set the values in the two variables you declared.

AJAX chat commands - if command doesn't exist, echo error

So I have this commands handling:
$message = the entered message.
public function handleCommands($message, $username)
{
//Variables we're going to use
$space = strpos($message, ' '); # The first space.
$command = trim(substr($message, 1, $space)); # The command after the slash
$name = substr($message, $space + 1); # The name after the command.
switch ($command)
{
case 'ban':
$this->ban($name, $username);
break;
case 'prune':
$this->prune($username);
break;
case '':
echo 'Please use a command!';
break;
case 'test':
try
{
$this->test($name);
}
catch (exception $r)
{
echo $r->getMessage();
}
break;
}
}
This basically will check for the command.
$command = the entered word after the slash ( " / " ).
Can you see
case '':
This basically checks if there is no command after the slash.
Question: I want the system to check aswell, if the command exists in the cases.
For example:
user wrote:
/hello
But that command doesn't exists, considering we only have case 'ban', case 'prune', case 'test' and case ''.
there is no case 'hello', so it will throw an error.
Is there a function that does this sort of thing? How can I do this?
I believe what you're looking for is a default: case.
Example:
<?php
switch ($i) {
case 0:
echo "i equals 0";
break;
case 1:
echo "i equals 1";
break;
case 2:
echo "i equals 2";
break;
default:
echo "i is not equal to 0, 1 or 2";
}
?>
EDIT:
Fixed version of problem that was chatted about: http://privatepaste.com/bd34e7e63b
Use the case default:
switch ($command)
{
case 'ban':
$this->ban($name, $username);
break;
case 'prune':
$this->prune($username);
break;
case '':
echo 'Please use a command!';
break;
case 'test':
try
{
$this->test($name);
}
catch (exception $r)
{
echo $r->getMessage();
}
break;
default:
echo "That command does not exist.";
}

Continue case in switch

I have following code:
switch(true)
{
case (something):
{
break;
}
case (something2):
{
break;
}
case (something3):
{
break;
}
}
Also the switch statement must check what one of cases give a TRUE, that is not the problem, the problem is, that i have now a case, where inside of case ... break; after checking other data, i wish to choose other switch-case, the one, that following him.
I have try do this:
switch(true)
{
case (something):
{
break;
}
case (something2):
{
if(check)
{
something3 = true;
continue;
}
break;
}
case (something3):
{
break;
}
}
But PHP do not wish to go in to the case (something3): its break the full switch statement. How i can pass the rest of code of one case and jump to the next?
This is what's called a "fall-through". Try organizing your code with this concept.
switch (foo) {
// fall case 1 through 2
case 1:
case 2:
// something runs for case 1 and 2
break;
case 3:
// something runs for case 3
break;
}
Using your code:
switch(true)
{
case (something):
{
break;
}
case (something2):
{
if(check) {
something3 = true;
}
else {
break;
}
}
case (something3):
{
break;
}
}
This will get to case something2 and run your check. If your check passes then it doesn't run the break statement which allows the switch to "fall through" and also do something3.
case (something2):
{
if(!check)
{
break;
}
}
Try this:
switch(true) {
case (something):
{
break;
{
case (something2):
{
if (!check) {
break;
}
}
case (something3):
{
break;
}
}
I had some similar situation and I came up with a solution which in your case would be like this:
switch(true)
{
case (something):
{
break;
}
case (something2):
{
if(!check)
{
break;
}
}
case (something3):
{
break;
}
}
You see that instead of checking the conditions to go to the next case, we checked if we should ignore the current one.

Categories