dousuiguang9328 2017-10-22 20:38
浏览 34

如何在PHP中使用POST而不是GET从超链接检索数据?

The following code working fine. but I prefer to use POST method instead of GET, if there is any way to do that?

<?php
$lang=$_GET['lang'];
$langArray=array('en','ar','ch');
$found=false;
if(in_array($lang,$langArray))
$found=true;
if(!$found)
$lang='en';

$xml=simplexml_load_file("languages.xml") or die("xml not found!");
$title=$xml->title->$lang;
$text=$xml->text->$lang;
?>

<h1><?php echo $title ?></h1>
<div><?php echo $text ?></div>
<div style="margin-top:50px;">

Languages:

<a href="?lang=en">English</a>
<a href="?lang=ar">Arabic</a>
<a href="?lang=ch">Chinese</a>
  • 写回答

5条回答 默认 最新

  • dongwu9170 2017-10-22 20:41
    关注

    As commented below, that is not a very good solution or elegant solution to this problem, as $_POST usually causes the browser to act funny if you keep posting content to it. It may even quit loading the website period. So $_GET should be just fine... You can always encrypt the URL so if anyone is looking at the connection may not realize what they are looking at.

    Yes, and no... At least not in the sense which I think you are trying to do, I may be wrong though. It will change the structure of whatever you are doing, to use $_POST you have to submit it via a form, as per PHP:

    $_POST:

    An associative array of variables passed to the current script via the HTTP POST method when using application/x-www-form-urlencoded or multipart/form-data as the HTTP Content-Type in the request.

    So you could in theory submit a form with the lang value, and change from

    $lang=$_GET['lang'];
    

    To:

    $lang=$_POST['lang'];
    

    So instead of having simple links, setup a form with a buttons, or a dropdown:

    <form id="web_lang" method="post">
        <button name="lang" value="en">English</button>
        <button name="lang" value="ar">Arabic</button>
        <button name="lang" value="ch">Chinese</button>
    </form>
    

    NOTE: Do not use $_GET or $_POST unfiltered, those are user inputs, and you NEVER trust user input, they can pass malicious code and do some serious harm to your website.

    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程