I have a php code that is meant to redirect people to a new page if they get the correct username and password. I have used a switch statement which has the header location function. The problem is that the header is executing for both cases and also the default keyword. I would only like the header to be excuted for one of the correct username and passwords
<?php
switch($_POST["username"] + "|" + $_POST["password"]) {
case"un1"|"pw1":
header("Location:http://MyFavWebsite.php");
break;
case"un2"|"pw2":
echo "hello";
break;
default:
echo "Go Away";
}
?>
I would like to know if this could be fixed i will appreciate any help to solve this problem.
case"un1"|"pw1":
Because that is not the right format for a string and it breaks your switch structure
case "un1|pw1":
Is.
And Oh someone might ask. That | is a bitwise OR operator and its result for your first case is TRUE that's why you always get that redirect.
header has no issue but your switch syntax is wrong, use both parameters as one condition
i.e
<?php
switch($_POST["username"].$_POST["password"]) {
case"un1pw1":
header("Location:http://MyFavWebsite.php");
break;
case"un2pw2":
echo "hello";
break;
default:
echo "Go Away";
}
//e.g
$un='un1';
$pass='pw1';
switch($un.$pass) {
case"un1pw1":
header("Location:http://MyFavWebsite.php");
break;
case"un2pw2":
echo "hello";
break;
default:
echo "Go Away";
}
?>
Try to change case"un1"|"pw1": to case "un1|pw1"
Related
i am trying to show information using IF/ELSEIF commands
i have A through Z along the top of the page and want to show a table with all results starting with each letter
for example i have
<a href='?a'>A</a>
<?php
if($_GET == a)
{
echo "<table><tr><th>[picture]</th><th>information</th></tr>";
}
?>
i want to show a table with all the information, how would i do this using IF/ELSE commands? is there a better way of doing this without going to a different page?
thanks in advance for any help
I think it would be easier/cleaner to use a switch-case instead of if-else for your purpose here.
First off, try changing the links to something like this:
<a href='?l=a'>A</a>
and
<a href='?l=b'>B</a>
Then you should try to access the chosen letter with something like this:
<?php
$sLetter = null;
if (isset($_GET['l'])) {
$sLetter = strtolower($_GET['l']);
}
switch ($sLetter) {
case 'a':
echo "Information related to A";
break;
case 'b':
echo "Information related to B";
break;
// Continue in a similar way for the remaining letters
default:
echo "No information..."; // or perhaps show all A-Z information
break;
}
Note: For testing purposes, this is okay. But Superglobals should always be validated and sanitised to make your application more secure.
i got the code below to find the url of the site and this works. But I want a switch statement to search this url in a list and echo something else in for every url.
$actual_link = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
As far as I understand the question, this looks like what you are looking for:
switch ($actual_link) {
case 'http://domain/something1/':
echo 'Something 1';
break;
case 'http://domain/something2/':
echo 'SomeSthing 2';
break;
default:
echo 'default';
}
I have used the following SWITCH statement to ECHO one of three CASE's. However when I do this the output to the web page shows the CASE label then the actual echo-ed statement.
$sc_stk_poa is a boolean field of either 0 or -1.
$sc_stk_prc_stanard is a price field in the MySQL database.
echo $x = $sc_stk_poa;
switch($x) {
case 0: echo "£{$sc_stk_prc_standard}";
break;
case -1: echo "POA";
break;
default: echo "";
}
The output I get in the webpage is something like: -1POA or 0£59.14 depending which case is selected to output.
Write less convoluted things :
<?php
switch ($sc_stk_poa)
{
case 0:
echo "£{$sc_stk_prc_standard}";
break;
case -1:
echo "POA";
break;
default:
echo "";
}
There was no need for variable $x, was it?
It's not displaying the label. You have an echo here: <?php echo $x=$sc_stk_poa;. This is the one outputting the value of x, which is used in your cases.
I have a series of PHP page, and I would like to use conditional logic to apply different rules to each page. Im not sure if my method is the best way to go about it, so I wanted to see if the community had any recommendations, as this doesn't feel like the best approach. Code Below:
<?php
$nameurl = $_SERVER["REQUEST_URI"];
if ($nameurl == "/fs/about.php"){
echo "about page";
}
elseif ($nameurl == "/fs/index.php"){
echo "home page";
}
?>
Ideally, I would like to only use the filename (index.php or about.php) instead of having /fs/. Im not sure if there is another way of using $_SERVER with PHP but it seems like there might be a more efficient and reusable way of writing this. Thoughts?
You could use
// get script name
$script = explode('/', $_SERVER['PHP_SELF']);
$scriptname = $script[count($script) - 1];
switch ($scriptname) {
case "index.php":
// Something you only want to show on this page
break;
case "about.php":
// Something you only want to show on this page
break;
}
To save a couple of lines of code, you could replace the multiple ifs with a switch:
http://php.net/manual/en/control-structures.switch.php
$nameurl = $_SERVER["REQUEST_URI"];
switch ($nameurl) {
case "/fs/about.php":
echo "about page";
break;
case "/fs/index.php":
echo "home page";
break;
default:
echo "unknown page";
break;
}
Makes it a little easier to add new cases in the future, but it's essentially doing the same thing...
There might be ways to make it more optimized, but I think if you start doing too much you lose the ability to easily understand what's happening in the code, so unless you comment what you're doing future people looking at your work will curse you. :P
Try this
$nameurl = basename($_SERVER['REQUEST_URI'], '.php');
echo $nameurl, " page";
http://php.net/manual/en/function.basename.php
You could try :
$currentFile = $_SERVER["PHP_SELF"];
$current_filename = explode('/', $currentFile);
or
$current_filename = basename($_SERVER['REQUEST_URI']) .'php';
I have:
one
And:
two
PHP page:
<?php
if mypage=one then
echo "my stuff text........one";
if mypage=two then
echo "my stuff text........two";
?>
I want to get text separately for each link from same php page
First of all, if then construct is not available in PHP so your code is syntactically wrong. The use of switch as suggested already is a good way to go. However, for your problem, you should use $_GET['mypage'] instead of $_POST['mypage']. It seems you are beginning PHP. Once you get some good basics, you will probably be making use of the functions such as include() and require(). Make sure you do not make mistakes beginners do:
<?php
if (isset($_GET['mypage'])
{
#include($_GET['mypage']);
}
?>
The above code works and looks simple but it has a very dangerous implementation allowing the malicious users to perform an attack known as file inclusion attack. So you should try to use the switch statements such as:
<?php
$mypage = $_GET['mypage']; //also you might want to cleanup $mypage variable
switch($mypage)
{
case "one":
#include("one.php");
break;
case "two":
#include("two.php");
break;
default:
#include("404.php");
}
?>
Umm, that php is not even remotely valid code. You want a switch statement:
<?php
$mypage = isset($_GET['mypage']) ? $_GET['mypage'] : '';
switch ($mypage) {
case 'one':
echo "my stuff text........one";
break;
case 'two':
echo "my stuff text........two";
break;
default:
header('HTTP/1.0 404 Not Found');
echo 'This page does not exist';
}