phpvariablesformspostget

How to give new life into a five years old, simple but reliable PHP form?


I have a script in php 5.2. I want to use a simple form. I found something a programmer made for me about 5 years ago. When I use it, PHP outputs an error now unless I set register_long_arrays = On, then it works fine.

On the PHP website, however, it says:

Warning This feature has been DEPRECATED as of PHP 5.3.0. Relying on this feature is highly discouraged. It's recommended to turn them off, for performance reasons. Instead, use the superglobal arrays, like $_GET.

Should I listen to PHP's warning, or just enable the option and keep using my old form happily? If the former, then how/where do I change this simple form, so it does not rely on the deprecated setting?

form.htm

<html><body>
<form method="POST" action="form_sent.php"> 
  ...   
</form>
</body></html>

form_sent.php

<html><body>
<?php
$email = $HTTP_POST_VARS[email];
$mailto = "info@website.com";
$mailsubj = "A Form was Sent from Website!";
$mailhead = "From: $email\n";
reset ($HTTP_POST_VARS);
$mailbody = "Values submitted from web site form:\n";
while (list($key, $val) = each ($HTTP_POST_VARS)){$mailbody .= "$key : $val\n";}
if (!eregi("\n",$HTTP_POST_VARS[email])) {
   mail($mailto, $mailsubj, $mailbody, $mailhead); }
?>

<b>Form Sent. Thank you.</b>
</body></html>

Solution

  • This is simple. Change where you have $HTTP_POST_VARS and use $_POST in its place.

    So, you will end up with:

    <html><body>
    <?php
    $email = $_POST[email];
    $mailto = "info@website.com";
    $mailsubj = "A Form was Sent from Website!";
    $mailhead = "From: $email\n";
    $mailbody = "Values submitted from web site form:\n";
    while (list($key, $val) = each ($_POST)){$mailbody .= "$key : $val\n";}
    if (!eregi("\n",$_POST[email])) {
       mail($mailto, $mailsubj, $mailbody, $mailhead); }
    ?>
    
    <b>Form Sent. Thank you.</b>
    </body></html>
    

    And, yes you should definitely listen to the PHP documentation and make these changes. I'm actually surprised those old superglobals haven't been removed already.

    Also, no need for the reset() part. It was likely leftover from an old version of this form using the internal array pointer, but you're not using the array pointer. You are using it as an associative array.