echo certain text from php links - 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';
}

Related

how can i use IF commands to show different information on a table?

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.

What is wrong with the header location function in php

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"

php: how to search the url in a switch case and echo always something different for every url?

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';
}

Using PHP to identify current page

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';

grab variable from url

i am new to php, but im trying. i need you guys help.
i have the following url in the browser address bar www.dome.com\mypage.php?stu=12234342
i am trying to pass the url from the main page to the select case page call select.php
if i should echo the url i get www.dome.com\select.php. so i have decided to echo $_SERVER['HTTP_REFERER']
instead, this gives me the correct url. how can i echo the variable from www.dome.com\mypage.php?stu=12234342 (12234342)
unto select.php. select.php contains code that needs the $var stu=12234342 in order to display the correct message.
$request_url=$_SERVER['HTTP_REFERER'] ; // takes the url from the browers
echo $request_url;
$cOption = $_GET['id'];
switch($cOption) {
case 1:
echo ' some text';
break;
case 2:
echo ' this page.php';
break;
case 3:
echo 'got it';
break;
default:
echo 'Whoops, didn\'t understand that option: <i>'.$cOption.'</i>';
}
?>
You may use parse_url() and parse_string() to grab the variable from a url:
<?php
//assuming www.dome.com/mypage.php?stu=12234342;
$url=$_SERVER['HTTP_REFERER'];
//parse the url to get the query_string-part
$parsed_url=parse_url($url);
//create variables from the query_string
parse_str($parsed_url['query'], $unsafe_vars);
//use the variables
echo $unsafe_vars['stu'];//outputs 12234342
?>
But note: you can't rely on the availability of HTTP_REFERER.
try
echo $_GET['stu'];
on select.php
That's why you need to call the select.php file like this:
www.dome.com/select.php?stu=12234342
and then you can add:
echo $_GET['stu'];
By the way, you need to research about XSS, because that's a huge vulnerability.

Categories