Jump to content

Review my code


mjza

Recommended Posts

Greetings Guys

I'm a complete newbie to PHP, just managed to get my head around HTML & CSS, so be kind. I need assistance with a PHP form on my site. Its allows tourist to Cape Town to book a rental cellphone. Got the basic script free from Bellonline (not sure if i should mention that on the forum), the basic script works perfectly. I 've tried to include more info, but it returns errors wrt user input.

 

The working script is here: www.squaremobile.co.za/contactus.php

I require some assistance with: www.squaremobile.co.za/rental.php

 

The config script:

<?php
/* 
BELLonline PHP MAILER SCRIPT v1.5
Copyright 2006 Gavin Bell 
http://www.bellonline.co.uk 
gavin@bellonline.co.uk

Set up an email form on your website within minutes.
Very simple to install and use, and fully customisable.

All you need to edit to get the script working is $sendto_email
but there are other options so that you can further customise the script.
*/

// Edit the following with the email address that you want the form to send to

$sendto_email = "contact@squaremobile.co.za";


// The settings below should be fine but you can edit them anyway

// Disable email addresses from the same domain as your email from being sent? 
// This will often reduce spam but will not allow antone to send from anything@yourdomain. 
$checkdomain = "yes";
// Language variables
$lang_title = "Rental a mobile package";
$lang_notice = "Fill in the form to order online by email. All fields are required";
$lang_name = "Your name";
$lang_country = "Your country";
$lang_passport = "Your passport number";
$lang_youremail = "Your email";
$lang_yourtelephone = "Your telephone";
$lang_unitsnumber = "Units required";
$lang_unitsstart = "Start date";
$lang_unitsend = "End date";
$lang_deliveryname = "Delivery name";
$lang_deliveryaddress = "Delivery address";
$lang_deliverytelephone = "Delivery telephone";
$lang_deliveryemail = "Delivery email";
$lang_confirmation = "Enter validation code";
$lang_submit = "Send email";
// Error messages
$lang_error = "Your email has not been sent, the following errors were found:";
$lang_noname = "You did not enter your name";
$lang_nocountry = "Please enter your country of origin";
$lang_nopassport = "We require your passport number";
$lang_noemail = "You did not enter your email address";
$lang_notelephone = "Please enter your telephone number";
$lang_nounitsnumber = "How many rental units are required";
$lang_nounitsstart = "Your arrival in Cape Town";
$lang_nounitsend = "Your departure from Cape Town";
$lang_nodeliveryname = "Your accomodation in Cape Town";
$lang_nodeliveryaddress = "Accomodation address in Cape Town";
$lang_nodeliverytelephone = "Telephone number of accomodation in Cape Town";
$lang_nodeliveryemail = "Email of accomodation in Cape Town";
$lang_nocode = "You did not enter the validation code";
$lang_wrongcode = "You entered the validation code incorrectly. Please note that it is case sensitive";
$lang_invalidemail = "The email address that you entered appears to be invalid";
// Success
$lang_sent = "Your email has been sent. The following message was submitted:";
// Width of form inputs. Must include unites, e.g px 
$input_width = "300px";
// How do you want the title aligned?
$title_align = "left"; // Can be left, center or right
// To format the title text. If you are not confident with css then probably best left as it is
$title_css = "font-weight: bold; font-size: 120%; padding:25px 0 10px 10px";
// Colour of error message
$error_colour = "red"; // Must use HTML compatible colour
// You can choose whether to display Powered by BELLonline PHP mailer script at the bottom of the mail form
// I understand that some peopme might not want to show our link, but we would appreciate it if you could 
// Possible options are yes or no
$showlink = "no";
// Thanks for using the PHP mailer script, I hope you find it useful!
?>

The actual form script:
<?php
/* 
    BELLonline PHP MAILER SCRIPT v1.5
    Copyright 2006 Gavin Bell 
    http://www.bellonline.co.uk 
    gavin@bellonline.co.uk

    Set up an email form on your website within minutes - see readme.txt for installation.
*/

extract($_POST);

if (!file_exists("config2.php")) 
    {
$host = $_SERVER[HTTP_HOST ];
$path = pathinfo($_SERVER['PHP_SELF']);
$file_path = $path['dirname'];
print "<h1>BELLonline PHP mailer script</h1>
    <h2>There is a problem with your PHP mailer script installation</h2>
    <p>The config.php file seems to be missing!</p>
    <p>For this script to work, you need to upload the config.php file that came with the download of the BELLonline <a href=\"http://bellonline.co.uk/downloads/php-mailer-script/\">PHP mailer script</a>.</p>
    <p>The file must be in the following directory of your website:</p>
    <p>$host<span style=\"font-weight: bold; font-size: 150%;\">$file_path/</span></p>
    <p>If you need help installing the script, then feel free to email me at <a href=\"&#x6d;&#x61;&#105;&#108;&#116;&#x6f;&#58;&#x67;&#x61;&#118;&#x69;&#110;&#64;&#98;&#x65;&#x6c;&#x6c;&#x6f;&#110;&#x6c;&#105;&#110;&#101;&#46;&#x63;&#111;&#46;&#x75;&#x6b;\">&#x67;&#x61;&#118;&#x69;&#110;&#64;&#98;&#x65;&#x6c;&#x6c;&#x6f;&#110;&#x6c;&#105;&#110;&#101;&#46;&#x63;&#111;&#46;&#x75;&#x6b;</a></p>";
exit;
    }
include "config2.php";


if ($sendto_email == "changeme@example.com")
    {
print "<h1>BELLonline PHP mailer script</h1>
    <h2>Installation nearly complete!</h2>
    <p>Thank you for downloading the <a href=\"http://bellonline.co.uk/downloads/php-mailer-script/\" title=\"free PHP mailer script\">free PHP mailer script</a> from <a href=\"http://www.bellonline.co.uk\">BELLonline web services</a>. </p>
    <p>To start using the script, open config.php in a text editor and change the <b>&#36;sendto_email</b> variable to your email address.</p>
    <p>If you did not get a config.php file with this script, then go to the <a href=\"http://bellonline.co.uk/downloads/php-mailer-script/\">PHP mailer script page</a> and download the full script.</p>
    <p>If you need help installing the script, then feel free to email me at <a href=\"&#x6d;&#x61;&#105;&#108;&#116;&#x6f;&#58;&#x67;&#x61;&#118;&#x69;&#110;&#64;&#98;&#x65;&#x6c;&#x6c;&#x6f;&#110;&#x6c;&#105;&#110;&#101;&#46;&#x63;&#111;&#46;&#x75;&#x6b;\">&#x67;&#x61;&#118;&#x69;&#110;&#64;&#98;&#x65;&#x6c;&#x6c;&#x6f;&#110;&#x6c;&#105;&#110;&#101;&#46;&#x63;&#111;&#46;&#x75;&#x6b;</a></p>";
exit;
    } 
if (empty ($senders_name)) 
    {
    $error = "1";
    $info_error .= $lang_noname . "<br>"; 
    }
if (empty ($senders_country)) 
    {
    $error = "1";
    $info_error .= $lang_nocountry . "<br>"; 
    }
if (empty ($senders_passport)) 
    {
    $error = "1";
    $info_error .= $lang_nopassport . "<br>"; 
    }
if (empty ($senders_email)) 
    {
    $error = "1";
    $info_error .= $lang_noemail . "<br>";  
    }
if (empty ($senders_yourtelephone)) 
    {
    $error = "1";
    $info_error .= $lang_notelephone . "<br>"; 
    }
if (empty ($mail_unitsnumber)) 
    {
    $error = "0";
    $info_error .= $lang_nounitsnumber . "<br>";  
    }
if (empty ($senders_unitsstart)) 
    {
    $error = "1";
    $info_error .= $lang_nounitsnumber . "<br>"; 
    }
if (empty ($senders_unitsend)) 
    {
    $error = "1";
    $info_error .= $lang_nounitsend . "<br>"; 
    }
if (empty ($senders_deliveryname)) 
    {
    $error = "1";
    $info_error .= $lang_nodeliveryname . "<br>"; 
    }
if (empty ($senders_deliveryaddress)) 
    {
    $error = "1";
    $info_error .= $lang_nodeliveryaddress . "<br>"; 
    }
if (empty ($senders_deliverytelephone)) 
    {
    $error = "1";
    $info_error .= $lang_nodeliverytelephone . "<br>"; 
    }
if (empty ($senders_deliveryemail)) 
    {
    $error = "1";
    $info_error .= $lang_nodeliveryemail . "<br>"; 
    }
if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,6}$", $senders_email))
    {
    $error = "1";
    $info_error .= $lang_invalidemail . "<br>";
    }
if (empty ($security_code))  
    {
    $error = "1";
    $info_error .= $lang_nocode . "<br>";  
    }
elseif ($security_code != $randomness)  
    {
    $error = "1";
    $info_error .= $lang_wrongcode . "<br>";  
    }
if ($showlink != "no")
    {
    $link = "<br><br><span style=\"font-size: 10px;\"> </span>";
    }
if ($error == "1") 
    {
    $info_notice = "<span style=\"color: " . $error_colour . "; font-weight: bold;\">" . $lang_error . "</span><br>"; 
    
    if (empty ($submit)) 
        {
        $info_error = "";
        $info_notice = $lang_notice;
        }   

    function Random() 
        {
        $chars = "ABCDEFGHJKLMNPQRSTUVWZYZ23456789";
        srand((double)microtime()*1000000);
        $i = 0;
        $pass = '' ;
        while ($i <= 4) 
            {
            $num = rand() % 32;
            $tmp = substr($chars, $num, 1);
            $pass = $pass . $tmp;
            $i++; 
            } 
        return $pass; 
        }
    $random_code = Random();
    $mail_message = stripslashes($mail_message);

    print "<form name=\"BELLonline_email\" method=\"post\" style=\"margin: 0;\" action=\"\">
  <table  border=\"0\" width=\"580\" cellspacing=\"0\" cellpadding=\"0\">
    <tr align=\"$title_align\" valign=\"top\">
      <td colspan=\"2\"><span style=\"$title_css\">$lang_title</span></td>
    </tr>
    <tr align=\"left\" valign=\"top\">
      <td colspan=\"2\" class=\"formN\">$info_notice$info_error</td>
    </tr>
    <tr valign=\"top\">
      <td width=\"200\" class=\"formL\">$lang_name</td>
      <td align=\"left\"><input name=\"senders_name\" type=\"text\" class=\"mailform_input\" id=\"senders_name\" style=\"width: $input_width;\" value=\"$senders_name\" maxlength=\"32\"></td>
    </tr>
    <tr valign=\"top\">
      <td width=\"200\" class=\"formL\">$lang_country</td>
      <td align=\"left\"><input name=\"senders_country\" type=\"text\" class=\"mailform_input\" id=\"senders_country\" style=\"width: $input_width;\" value=\"$senders_country\" maxlength=\"32\"></td>
    </tr>
    <tr valign=\"top\">
      <td width=\"200\" class=\"formL\">$lang_passport</td>
      <td align=\"left\"><input name=\"senders_passport\" type=\"text\" class=\"mailform_input\" id=\"senders_passport\" style=\"width: $input_width;\" value=\"$senders_passport\" maxlength=\"32\"></td>
    </tr>
    <tr valign=\"top\">
      <td width=\"200\" class=\"formL\">$lang_youremail</td>
      <td align=\"left\"><input name=\"senders_youremail\" type=\"text\" class=\"mailform_input\" id=\"senders_youremail\" style=\"width: $input_width;\" value=\"$senders_youremail\" maxlength=\"64\"></td>
    </tr>
    <tr valign=\"top\">
      <td width=\"200\" class=\"formL\">$lang_yourtelephone</td>
      <td align=\"left\"><input name=\"senders_yourtelephone\" type=\"text\" class=\"mailform_input\" id=\"senders_yourtelephone\" style=\"width: $input_width;\" value=\"$senders_yourtelephone\" maxlength=\"32\"></td>
    </tr>
    <tr valign=\"top\">
      <td width=\"200\" class=\"formL\">$lang_unitsnumber</td>
      <td align=\"left\"><input name=\"senders_unitsnumber\" type=\"text\" class=\"mailform_input\" id=\"senders_unitsnumber\" style=\"width: $input_width;\" value=\"$senders_unitsnumber\" maxlength=\"3\"></td>
    </tr>
    <tr valign=\"top\">
      <td width=\"200\" class=\"formL\">$lang_unitsstart</td>
      <td align=\"left\"><input name=\"senders_unitsstart\" type=\"text\" class=\"mailform_input\" id=\"senders_unitsstart\" style=\"width: $input_width;\" value=\"$senders_unitsstart\" maxlength=\"32\"></td>
    </tr>
    <tr valign=\"top\">
      <td width=\"200\" class=\"formL\">$lang_unitsend</td>
      <td align=\"left\"><input name=\"senders_unitsend\" type=\"text\" class=\"mailform_input\" id=\"senders_unitsend\" style=\"width: $input_width;\" value=\"$senders_unitsend\" maxlength=\"32\"></td>
    </tr>
    <tr valign=\"top\">
      <td width=\"200\" class=\"formL\">$lang_deliveryname</td>
      <td align=\"left\"><input name=\"senders_deliveryname\" type=\"text\" class=\"mailform_input\" id=\"senders_deliveryname\" style=\"width: $input_width;\" value=\"$senders_deliveryname\" maxlength=\"32\"></td>
    </tr>
    <tr valign=\"top\">
      <td width=\"200\" class=\"formL\">$lang_deliveryaddress</td>
      <td align=\"left\"><input name=\"senders_deliveryaddress\" type=\"text\" class=\"mailform_input\" id=\"senders_deliveryaddress\" style=\"width: $input_width;\" value=\"$senders_deliveryaddress\" maxlength=\"32\"></td>
    </tr>
    <tr valign=\"top\">
      <td width=\"200\" class=\"formL\">$lang_deliverytelephone</td>
      <td align=\"left\"><input name=\"senders_deliverytelephone\" type=\"text\" class=\"mailform_input\" id=\"senders_deliverytelephone\" style=\"width: $input_width;\" value=\"$senders_deliverytelephone\" maxlength=\"32\"></td>
    </tr>
    <tr valign=\"top\">
      <td width=\"200\" class=\"formL\">$lang_deliveryemail</td>
      <td align=\"left\"><input name=\"senders_deliveryemail\" type=\"text\" class=\"mailform_input\" id=\"senders_deliveryemail\" style=\"width: $input_width;\" value=\"$senders_deliveryemail\" maxlength=\"32\"></td>
    </tr>
    <tr align=\"left\" valign=\"top\">
      <td width=\"200\" class=\"formL\">$lang_confirmation</td>
      <td><input name=\"security_code\" type=\"text\" id=\"security_code\" size=\"5\"> 
            <b>$random_code</b></td>
    </tr>
    <tr valign=\"top\">
      <td colspan=\"2\" align=\"right\"><input name=\"randomness\" type=\"hidden\" id=\"randomness\" value=\"$random_code\">
      <input name=\"submit\" type=\"submit\" id=\"submit\" value=\"$lang_submit\" class=\"mailform_button\"></td>
    </tr>
  </table>
</form>";
    }
else
    {
    
    
    
    if ($checkdomain == "yes") 
        {
        $sender_domain = substr($senders_email, (strpos($senders_email, '@')) +1);
        $recipient_domain = substr($sendto_email, (strpos($sendto_email, '@')) +1);
        if ($sender_domain == $recipient_domain)
            {
            print "Sorry, you cannot send messages from this domain ($sender_domain)";
            exit;
            }       
        }
        
        
    $info_notice = $lang_sent;
    $mail_message = stripslashes($mail_message);
    $senders_email = preg_replace("/[^a-zA-Z0-9s.@-_]/", "-", $senders_email);
    $senders_name = preg_replace("/[^a-zA-Z0-9s]/", " ", $senders_name);
    $headers = "From: $senders_name <$senders_email> \r\n";
    $headers .= "X-Mailer: BELLonline.co.uk PHP mailer \r\n";
    mail($sendto_email, $mail_subject, $mail_message, $headers);
    print "  <table  border=\"0\" cellspacing=\"2\" cellpadding=\"2\">
    <tr align=\"$title_align\" valign=\"top\">
      <td colspan=\"2\"><span style=\"$title_css\">$lang_title</span></td>
    </tr>
    <tr align=\"$title_align\" valign=\"top\">
      <td colspan=\"2\">$info_notice</td>
    </tr>
    <tr valign=\"top\">
      <td width=\"100\" align=\"right\">$lang_name</td>
      <td align=\"left\"><b>$senders_name</b></td>
    </tr>
    <tr valign=\"top\">
      <td width=\"100\" align=\"right\">$lang_country</td>
      <td align=\"left\"><b>$senders_country</b></td>
    </tr>
    <tr valign=\"top\">
      <td width=\"100\" align=\"right\">$lang_passport</td>
      <td align=\"left\"><b>$senders_passport</b></td>
    </tr>
    <tr valign=\"top\">
      <td width=\"100\" align=\"right\">$lang_youremail</td>
      <td align=\"left\"><b>$senders_email</b></td>
    </tr>
    <tr valign=\"top\">
      <td width=\"100\" align=\"right\">$lang_yourtelephone</td>
      <td align=\"left\"><b>$senders_yourtelephone</b></td>
    </tr>
    <tr valign=\"top\">
      <td width=\"100\" align=\"right\">$lang_unitsnumber</td>
      <td align=\"left\"><b>$sender_unitsnumber</b></td>
    </tr>
    <tr valign=\"top\">
      <td width=\"100\" align=\"right\">$lang_unitsstart</td>
      <td align=\"left\"><b>$sender_unitsstart</b></td>
    </tr>
    <tr valign=\"top\">
      <td width=\"100\" align=\"right\">$lang_unitsend</td>
      <td align=\"left\"><b>$sender_unitsend</b></td>
    </tr>
    <tr valign=\"top\">
      <td width=\"100\" align=\"right\">$lang_deliveryname</td>
      <td align=\"left\"><b>$senders_deliveryname</b></td>
    </tr>
    <tr valign=\"top\">
      <td width=\"100\" align=\"right\">$lang_deliveryaddress</td>
      <td align=\"left\"><b>$senders_deliveryaddress</b></td>
    </tr>
    <tr valign=\"top\">
      <td width=\"100\" align=\"right\">$lang_deliverytelephone</td>
      <td align=\"left\"><b>$senders_deliverytelephone</b></td>
    </tr>
    <tr valign=\"top\">
      <td width=\"100\" align=\"right\">$lang_deliveryemail</td>
      <td align=\"left\"><b>$senders_deliveryemail</b></td>
    </tr>
  </table>";
    }
print $link;
?>

 

Looking forward to learning more PHP. Please ensure that explanations are simple, like a 7 year old. Much appreciated  ;)

mj

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.