Help new to PHP

Discussion in 'PHP' started by webmaster@newberryautomot, May 5, 2007.

  1. #1
    ok here it is I am having a problem with a simple messaging page that I wrote. Here is the code below. When I try to load the page it loads blank. I really need any help that you can give.

    I have the file permissions set on the server to 774.

    Here is the message.php code:

    <?php
    include "message.inc";
    $contact = new contactform();
    $contact->DisplayForm();
    ?>

    Here is the message.inc code:

    <?php
    // First, make sure the form was posted from a browser.
    // For basic web-forms, we don't care about anything
    // other than requests from a browser:
    if(!isset($_SERVER['HTTP_USER_AGENT'])){
    die("Forbidden - You are not authorized to view this page");
    exit;
    }
    // Attempt to defend against header injections:
    $badStrings = array("Content-Type:",
    "MIME-Version:",
    "Content-Transfer-Encoding:",
    "bcc:",
    "cc:");
    // Loop through each POST'ed value and test if it contains
    // one of the $badStrings:
    foreach($_POST as $k => $v){
    foreach($badStrings as $v2){
    if(strpos($v, $v2) !== false){
    logBadRequest();
    header("HTTP/1.0 403 Forbidden");
    exit;
    }
    }
    }

    // Made it past spammer test, free up some memory
    // and continue rest of script:

    unset($k, $v, $v2, $badStrings);

    class contactform
    {
    var $toaddress;
    var $name;
    var $email;
    var $subject;
    var $message;
    var $errmsg;
    var $error;
    var $outmessage;
    var $headers;

    function contactform()
    {
    $this->toaddress = "webmaster@newberryautomotive.com";
    // $this->toaddress = "webmaster@newberryautomotive.com,rnewby22@yahoo.com";

    if(isset($_POST['sendmail']))
    $this->parseform();
    $this->debugLog();
    $this->outmessage = "";
    $this->headers = "";
    $this->email = "";

    function parseform()
    {
    if ($_POST['email'])
    {
    $this->email = $_POST['email'];
    }
    else
    {
    $this->error = 1;
    $this->errmsg = "You must enter your email addres.";
    }
    if ($_POST['name'])
    {
    $this->name = $_POST['name'];
    if (!$this->error)
    {
    $this->headers .= "From: " . $this->email . "\r\n";
    // $this->senders .= "From: " . $this->email . " (" . $this->name . ")\r\n";
    // $this->headers .= "From: " . $this->name . " <" . $this->email . ">\r\n";
    $this->outmessage .= "From: " . $this->name . "\n";
    // $this->outmessage .= "Email: " . $this->email . "\n";
    }
    }
    else
    {
    $this->error = 1;
    $this->errmsg = "You must enter your name.";
    }
    if ($_POST['message'])
    {
    $this->subject .= "Message From: " .$this->name;
    $this->message = $_POST['message'];
    $this->outmessage .= "Message:\n" . $this->message;
    }
    else
    {
    $this->error = 1;
    $this->errmsg = "You must enter your message.";
    }
    if (!$this->error)
    {
    if (!preg_match("/Content\-Type\:/i", $this->headers))
    // if ($this->subject)
    // if (!strstr($this->subject,$this->toaddress))
    {
    // if (mail($this->email, $this->toaddress, $this->subject, $this->outmessage)) //doesn't work!
    if (mail($this->toaddress, $this->subject, $this->outmessage, $this->email)) //this works but uses my email for the From: header.
    // if (mail($this->toaddress, $this->subject, $this->headers, $this->outmessage))
    // if (mail($this->headers, $this->toaddress, $this->subject, $this->outmessage))
    {
    $this->errmsg = "Your message was sucessfully sent. Do NOT Refresh this page!";
    }
    else
    {
    $this->errmsg = "Your message was <b>NOT</b> sent. " . $this->email . " Was not accepted. Please try again later.";
    }
    }
    }
    $this->debugLog();
    }

    function DisplayForm()
    {
    ?>

    <br>
    <table width="90%" border="0" cellspacing="0" cellpadding="0" align="center">
    <tr>
    <td colspan="2" align="left" valign="top"><?php
    if (isset($this->errmsg))
    {
    print '<h3><center><b>' . $this->errmsg . '</h3></center></b><p><br>';
    }
    ?>
    <form action="feedback2.php" method="post" name="FormName" enctype="application/x-www-form-urlencoded">
    <font face="arial" color="black" size="3"><center>

    <b>* Name:</b><br>
    <input type="text" name="name" size="34" border="0">
    </dl>

    <dl>
    <b>* Email Address:</b><br>
    <input type="text" name="email" size="34" border="0"><p>
    </dl>

    <dl>
    <b>* Message</b>:<br>
    <textarea name="message" rows="12" cols="54"></textarea><p><p>
    </dl>

    <input type="submit" name="sendmail" value="Send Message" border="0">
    <input type="reset" value="Clear Form">
    </dl>
    </form><br>
    <center>* A required field.</center>
    </td>
    </tr>
    </table>
    <br>
    <br>
    <?php
    }

    function debugLog()
    {
    if (!($fp = fopen('/tmp/debug.txt', 'w')))
    return;

    $this->showvalues("ENV", &$_ENV, $fp);
    $this->showvalues("SESSION", &$_SESSION, $fp);
    $this->showvalues("COOKIE", &$_COOKIE, $fp);
    $this->showvalues("SERVER", &$_SERVER, $fp);
    $this->showvalues("POST", &$_POST, $fp);
    $this->showvalues("GET", &$_GET, $fp);

    fclose($fp);
    }

    function showvalues($prefix, $array, $file)
    {
    foreach($array as $key => $value)
    {
    fwrite($file, sprintf("%s: %s => %s\n", $prefix, $key, $value));
    // fwrite($file, sprintf("%s: %s %s (%s)%s-%s", $toaddress, $name, $email, $phoneareacode, $phoneprefix, $phonesuffix));
    // fwrite($file, sprintf("%s: %s %s %s %s \n", $messagetype, $subject, $comment, $errmsg, $error));
    }
    }
    }

    ?>

    and the address of the page so you can see what it does:

    http:// www. newberryautomotive. com/ message.php

    if you have any idea as to why it doesn't work it would greatly be appreciated.