Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

PHP: $HTTP_GET_VARS [非推奨]の書き換え

$HTTP_GET_VARSは非推奨になっています。

しかし、古いプログラムでは使われています。
例えば、

test_args.php
<?php
// -----------------------------------------------------------------
echo "<html>";
echo '<meta http-equiv="CONTENT-TYPE" content="text/html; charset=utf-8" />';
echo "<body>";
echo "test_args.php<p />";
echo "<h2>テスト</h2>";
$in_vars = "";
while (list($key, $val) = each($HTTP_GET_VARS)) {
    $in_vars    .= $key."=".$val. "<p />";
}
echo $in_vars;
echo "Jun/28/2020<p />";
echo "</body>";
echo "</html>";
// -----------------------------------------------------------------
?>

このプログラムを走らせると、次のようなログが出ます。

/var/log/apache2/error.log
[Sun Jun 28 20:40:57.834991 2020] [php7:warn] [pid 8012] [client 127.0.0.1:41472] PHP Warning:  Variable passed to each() is not an array or object in /home/uchida/html/data_base_language/test_dir/php/test_args.php on line 10, referer: http://localhost/html/data_base/test_dir/php/

次のように修正すれば、正常に動きます。

<?php
// -----------------------------------------------------------------
echo "<html>";
echo '<meta http-equiv="CONTENT-TYPE" content="text/html; charset=utf-8" />';
echo "<body>";
echo "test_args.php<p />";
echo "<h2>テスト</h2>";
$in_vars = "";

// while (list($key, $val) = each($HTTP_GET_VARS)) {
while (list($key, $val) = each($_GET)) {
    $in_vars    .= $key."=".$val. "<p />";
}
echo $in_vars;
echo "Jun/28/2020<p />";
echo "</body>";
echo "</html>";
// -----------------------------------------------------------------
?>

次のように引数を与えて実行した結果です。
test_args.php?aa=12&bb=34&cc=56

test_args_jun28.png

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away