简体   繁体   中英

echo certain text from php links

I have:

<a href="index.php?mypage=one">one</a>

And:

<a href="index.php?mypage=two">two</a>

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

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

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");
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM