简体   繁体   中英

How to retrieve data from a hyperlink using POST instead of GET in PHP?

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>

Making links do POST requests can be done by two means, one is simply to make the link send a form, like this:

<form method=POST id=f1>
<input name=hidden value=whatever>
</form>
<a href='#' onclick='document.getElementById("f1").submit()' />clickme</a>

The other one is to have the js link send XMLHttpRequest or the new js fetch () api to send the data.

The usability downside of this approach, is that people cannot share the links or open in new tab, which is common.

Let's let links be links and forms be forms.

As per the security aspect of it, both GET and POST are unsafe, if you aren't using HTTPS, remember GET are supposed to be idempotent and POST requires reconfirmation upon browser refresh, further reducing usability.

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.

 <?php
    $lang=$_POST['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:

<form method="post" action="">
    <select name="lang">

        <option VALUE=""> choose language </option>
        <option VALUE="en"> English</option>
        <option VALUE="ar"> Arabic</option>
        <option VALUE="ch"> Chinese</option>
    </select>
<INPUT TYPE="submit" name="submit" />

While superficially it may seem like all one needs to do is change "$_GET" to "$_POST" in the OPs code, this kind of change is hardly trivial.

One must either create a form for submission via the POST method or make an HTTP POST request. If you choose the former, then your link would refer to the webpage containing the form. If you opt for the later, then your link would refer to a PHP script which may use fsockopen() or curl. Here's some example code from this discussion offered by Tamlyn that illustrates the mechanics with fsockopen():

<?php

$fp = fsockopen('example.com', 80);

$vars = array(
    'hello' => 'world'
);
$content = http_build_query($vars);

fwrite($fp, "POST /reposter.php HTTP/1.1\r\n");
fwrite($fp, "Host: example.com\r\n");
fwrite($fp, "Content-Type: application/x-www-form-urlencoded\r\n");
fwrite($fp, "Content-Length: ".strlen($content)."\r\n");
fwrite($fp, "Connection: close\r\n");
fwrite($fp, "\r\n");

fwrite($fp, $content);

header('Content-type: text/plain');
while (!feof($fp)) {
    echo fgets($fp, 1024);
}

Note, you need to arrange your data into an array of key-value pairs to make the HTTP POST request. Alternatively, you may find it easier to use AJAX; see this example .

因为您可以加密URL和不敏感的数据,所以我建议继续使用_GET方法,它比_POST方法快

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