LoginSignup
0
0

More than 3 years have passed since last update.

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

Last updated at Posted at 2020-06-28

$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

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0