简体   繁体   English

从PHP链接回显某些文本

[英]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页面:

<?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 我想分别从同一PHP页面获取每个链接的文本

Umm, that php is not even remotely valid code. 嗯,那个php甚至不是远程有效的代码。 You want a switch statement : 您需要一个switch语句

<?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. 首先, 如果then构造在PHP中不可用,那么您的代码在语法上是错误的。 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']. 但是,对于您的问题,应使用$ _GET ['mypage']而不是$ _POST ['mypage']。 It seems you are beginning PHP. 看来您正在开始使用PHP。 Once you get some good basics, you will probably be making use of the functions such as include() and require(). 一旦掌握了一些良好的基础知识,您就可能会使用诸如include()和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: 因此,您应该尝试使用switch语句,例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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