Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Page 1 of 2 1 2 LastLast
Results 1 to 10 of 12

Thread: PHP4 vs. PHP5

  1. #1
    Senior Member ravensjeff's Avatar
    Join Date
    Aug 2007
    Location
    Maryland
    Posts
    125
    Member #
    15751
    Hi there,

    I'm still fairly green in the world of PHP. I'm hoping someone can tell me why this code below works with PHP4 but not PHP5. Basically, this code emails the results of a submitted form. The variables come from the form elements.

    PHP Code:
    <?php

    if ($search == "do") {
    //add From: header
    $headers "From: " $name ."\r\n";

    //specify MIME version 1.0
    $headers .= "MIME-Version: 1.0\r\n";

    //unique boundary
    $boundary uniqid("HTMLDEMO");

    //tell e-mail client this e-mail contains//alternate versions
    $headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n";

    //plain text version of message
    //$body = "--$boundary\r\n" .
    //   "Content-Type: text/plain; charset=ISO-8859-1\r\n" .
    //   "Content-Transfer-Encoding: base64\r\n\r\n";
    //$body .= chunk_split(base64_encode("This is the plain text version!"));

    //HTML version of message
    $message "Dear Sir,<br/><br/>";
    $message .= $comments;
    $message .= "<br/><br/>Sincerely,<br/><br/>";
    $message .= $name "<br/>";
    $message .= "</br>" $email;
    $message .= "</br>" $phone;

    $body "--$boundary\r\n" .
       
    "Content-Type: text/html; charset=ISO-8859-1\r\n" .
       
    "Content-Transfer-Encoding: base64\r\n\r\n";
    $body .= chunk_split(base64_encode($message));

    //send message to recipient
    mail("sample@email.com""Website Contact Form Response"$body$headers);

    }
    ?>

  2.  

  3. #2
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    You're likely using an outdated version of PHP4. All newer versions of PHP have the register_globals setting turned off. This means that form values are not automatically bound to variables. Instead, you have to use $_GET and $_POST to get to them. You're probably submitting a POST request, so where you'd usually do, say, $name, you'd do $_POST['name']. Similarly, where you do $search you'd probably do $_GET['search'], as that's a GET variable. You may be able to use $_FORM to get to them without worrying about GET vs POST.

  4. #3
    Senior Member seanmiller's Avatar
    Join Date
    Sep 2003
    Location
    Glastonbury, UK
    Posts
    868
    Member #
    3263
    Liked
    1 times
    Yeah, if you have a lot of scripts like this and don't have time to re-write them you could create a file in the directory called .htaccess and use it to switch register_globals back on...

    Code:
    php_flag register_globals on
    ...but it's a security risk so it would be better to go through and change the code to use $_POST['variable_name']...

    Sean

  5. #4
    Senior Member ravensjeff's Avatar
    Join Date
    Aug 2007
    Location
    Maryland
    Posts
    125
    Member #
    15751
    Thank you for your replies...so to make sure I am understanding you correctly...the more appropriate coding would look like this?:

    PHP Code:
    <?php

    if ($_GET['search'] == "do") {
    //add From: header
    $headers "From: " $_POST['name'] ."\r\n";

    //specify MIME version 1.0
    $headers .= "MIME-Version: 1.0\r\n";

    //unique boundary
    $boundary uniqid("HTMLDEMO");

    //tell e-mail client this e-mail contains//alternate versions
    $headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n";

    //plain text version of message
    //$body = "--$boundary\r\n" .
    //   "Content-Type: text/plain; charset=ISO-8859-1\r\n" .
    //   "Content-Transfer-Encoding: base64\r\n\r\n";
    //$body .= chunk_split(base64_encode("This is the plain text version!"));

    //HTML version of message
    $message "Dear Sir,<br/><br/>";
    $message .= $_POST['comments'];
    $message .= "<br/><br/>Sincerely,<br/><br/>";
    $message .= $_POST['name'] . "<br/>";
    $message .= "</br>" $_POST['email'];
    $message .= "</br>" $_POST['phone'];

    $body "--$boundary\r\n" .
       
    "Content-Type: text/html; charset=ISO-8859-1\r\n" .
       
    "Content-Transfer-Encoding: base64\r\n\r\n";
    $body .= chunk_split(base64_encode($message));

    //send message to recipient
    mail("sample@email.com""Website Contact Form Response"$body$headers);

    }
    ?>

  6. #5
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    At a glance, yes, that should do it.

  7. #6
    Senior Member Shani's Avatar
    Join Date
    Nov 2004
    Posts
    1,140
    Member #
    8171
    Another way of doing it is to start your script by attaching values to your variables. It makes it so there is less to type each time, which always makes me happy!

    PHP Code:
    $search $_GET['search'];
    $name $_POST['name'];
    $comments $_POST['comments'];

    if(
    $search == "do" ){
      
    $headers "From" $name ... 
    Shani

    I have an eye for detail like you'd never believe.

  8. #7
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    Yes, but if you only use the variables once, then you're actually doing more typing :-D

  9. #8
    Senior Member seanmiller's Avatar
    Join Date
    Sep 2003
    Location
    Glastonbury, UK
    Posts
    868
    Member #
    3263
    Liked
    1 times
    Quote Originally Posted by Shadowfiend
    Yes, but if you only use the variables once, then you're actually doing more typing :-D
    In the 20 years I've been programming professionally I've heard this debate time and time again, over all different technologies.

    Personally I'd do it as DCScene suggests and it's not nothing to do with typing but rather with documentation and maintainability... the great advantage of defining the variables up top is that then anybody looking at the code can immediately see exactly what variables get passed in, rather than having to trawl through the code and find them one by one.

    I remember a great workshop hosted by Oracle guru Stephen Feuerstein about 10 years ago on self-documenting PL/SQL code.... creating functions to make the code read like a book was another of his suggestions...

    eg.
    Code:
    if ( customerRequestsADrink($brand) ) {
         if ( customerOverEighteen() )
              sellHimADrink($brand);
        else  tellHimNo();
    }
    ...that sort of thing.

    Anyway, strayed off topic so that'll do me for now... just something to ponder...

    Sean

  10. #9
    Senior Member ravensjeff's Avatar
    Join Date
    Aug 2007
    Location
    Maryland
    Posts
    125
    Member #
    15751
    Thanks again for your help fellas. I went back and started over with the script and defined my variables up front as discussed. The script is now better, and more encompassing, and works like a charm.

    Once again, I appreciate you guys steering me in the right direction.

  11. #10
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    Mm, that's the literate programming argument. It's a good one, and I tend to follow it, especially in Ruby. Nonetheless, in PHP, where your variables come from is the very least of your maintainability worries. There are far more severe things that can go wrong -- like how 99.9% of PHP pages are coded -- that should probably take precedence. Besides, it's like the good old idea of declaring your variables at the very top of any function or method in a statically typed language. You sometimes lose in flow more than you gain in maintainability. And the best argument, I think, is that if you're only going to be using your varaibles once, it'll probably be a relatively short page anyway. it's really a case-by-case decision, in the end.


Page 1 of 2 1 2 LastLast

Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 05:01 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com