# PHP script wont execute...



## brodie1600 (Aug 19, 2008)

I'm trying to setup a simple email script with PHP, and have it execute in a .php document, as well as having some html.
Here's how the script looks: (search "<?php" to find where it starts)


```
<head>
<title>We The Bakery - Contact Us</title>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1254">
<META NAME="audience" CONTENT="all">
<META NAME="Robots" CONTENT="INDEX,FOLLOW">
<LINK REL="SHORTCUT ICON"
       HREF="/images/logos/webico.ico">
<style type="text/css">
<!--
    body {
        margin:0;
        padding:0;
        font: bold 11px/1.5em Verdana;
}

h2 {
        font: bold 14px Verdana, Arial, Helvetica, sans-serif;
        color: #000;
        margin: 0px;
        padding: 0px 0px 0px 15px;
}

img {
border: none;
}

/*- Menu Tabs 10--------------------------- */

    #tabs10 {
      float:left;
      width:100%;
      font-size:93%;
          border-bottom:1px solid #000000;
      line-height:normal;
      }
    #tabs10 ul {
          margin:0;
          padding:10px 10px 0 50px;
          list-style:none;
      }
    #tabs10 li {
      display:inline;
      margin:0;
      padding:0;
      }
    #tabs10 a {
      float:left;
      background:url("Websites/We The Bakery/tableft10.gif") no-repeat left top;
      margin:0;
      padding:0 0 0 4px;
      text-decoration:none;
      }
    #tabs10 a span {
      float:left;
      display:block;
      background:url("Websites/We The Bakery/tabright10.gif") no-repeat right top;
      padding:5px 15px 4px 6px;
      color:#FFF;
      }
    /* Commented Backslash Hack hides rule from IE5-Mac \*/
    #tabs10 a span {float:none;}
    /* End IE5-Mac hack */
    #tabs9 a:hover span {
      color:#FFF;
      }
    #tabs10 a:hover {
      background-position:0% -42px;
      }
    #tabs10 a:hover span {
      background-position:100% -42px;
      }

      #tabs10 #current a {
              background-position:0% -42px;
      }
      #tabs10 #current a span {
              background-position:100% -42px;
      }
-->
</style>
<link rel="stylesheet" type="text/css" href="Websites/We The Bakery/style.css">
</head>

<body bgcolor="#FFEFFF" background="Websites/We The Bakery/images/background.jpg">

<div align="center">
	<table border="0" width="800" id="table1" cellspacing="0" cellpadding="0" bgcolor="#000000" style="border-left: 3px ridge #766B00; border-right: 3px ridge #766B00; ">
		<tr>
			<td colspan="3">
			<img border="0" src="Websites/We The Bakery/images/logos/banner1.jpg" width="800" height="164"></td>
		</tr>
		<tr>
			<td width="800" colspan="3" style="background-color: #5B2B0A">                <div id="tabs10">
                <ul>
                                <!-- CSS Tabs -->
<li><a href="Websites/We The Bakery/index.html"><span>Home</span></a></li>
<li><a href="Websites/We The Bakery/videos.html"><span>Videos</span></a></li>
<li><a href="http://wethebakery.myfreeforum.org"><span>Forums</span></a></li>
<li><a href="Websites/We The Bakery/downloads.html"><span>Downloads</span></a></li>
<li><a href="Websites/We The Bakery/contact.html"><span>Contact Us</span></a></li>
<li><a href="Websites/We The Bakery/about.html"><span>About</span></a></li>


                        </ul>
                </div></td>
		</tr>
		<tr>
			<td width="378">&nbsp;</td>
			<td width="211">&nbsp;</td>
			<td width="211">
			</td>
		</tr>
		<tr>
			<td width="800" valign="top" style="padding-left: 6px" colspan="3" height="129">
		  <p><font size="4"><b>Contact Us</b></font><br>
		    <br><center>
		      <font color="#FF00FF">Alternatively, instead of using the contact form below, you can simply email us at the email address <a href="mailto:[email protected]">[email protected]</a>. We do please that you check <a href="Websites/We The Bakery/emailhelp.html">this page</a> to be able to setup your email properly. Thanks!<br></font></center><br>
              
              <?php
session_start();
/*******************************************************************************
*  Title: Easy PHP Contact Form (Non Captcha Version)
*  Version: 1.1 @ October 23, 2009
*  Author: Vishal P. Rao
*  Website: http://www.easyphpcontactform.com
********************************************************************************
*  COPYRIGHT NOTICE
*  Copyright 2009 Vishal P. Rao. All Rights Reserved.
*
*  This script may be used and modified free of charge by anyone
*  AS LONG AS COPYRIGHT NOTICES AND ALL THE COMMENTS REMAIN INTACT.
*  By using this code you agree to indemnify Vishal P. Rao or 
*  www.easyphpcontactform.com from any liability that might arise from 
*  it's use.
*
*  Selling the code for this program, in part or full, without prior
*  written consent is expressly forbidden.
*
*  Obtain permission before redistributing this software over the Internet
*  or in any other medium. In all cases copyright and header must remain
*  intact. This Copyright is in full effect in any country that has
*  International Trade Agreements with the India
*
*  Removing any of the copyright notices without purchasing a license
*  is illegal! 
*******************************************************************************/

/*******************************************************************************
 *	Script configuration - Refer README.txt
*******************************************************************************/

/* Email address where the messages should be delivered */
$to = '[email protected]';

/* From email address, in case your server prohibits sending emails from addresses other than those of your 
own domain (e.g. [email protected]). If this is used then all email messages from your contact form will appear 
from this address instead of actual sender. */
$from = '';

/* This will be appended to the subject of contact form message */
$subject_prefix = 'WeTheBakery';

/* Security question and answer array */
$question_answers = array (
'2+2' => '4',
'2-2' => '0',
'1+1' => '2',
'1-1' => '0'
);

/* Form header file */
$header_file = 'form-header.php';

/* Form footer file */
$footer_file = 'form-footer.php';

/* Form width in px or % value */
$form_width = '70%';

/* Form background color */
$form_background = '#000000';

/* Form border color */
$form_border_color = '#666666';

/* Form border width */
$form_border_width = '1px';

/* Form border style. Examples - dotted, dashed, solid, double */
$form_border_style = 'solid';

/* Form cell padding */
$cell_padding = '5px';

/* Form left column width */
$left_col_width = '25%';

/* Form font size */
$font_size = '12px';

/* Empty/Invalid fields will be highlighted in this color */
$field_error_color = '#FF0000';

/* Thank you message to be displayed after the form is submitted. Can include HTML tags. Write your message 
between <!-- Start message --> and <!-- End message --> */
$thank_you_message = <<<EOD
<!-- Start message -->
<p>Your message has been sent.</p><br /><br /><br /><br /><br /><br /><br /><br />
<!-- End message -->
EOD;

/* URL to be redirected to after the form is submitted. If this is specified, then the above message will 
not be shown and user will be redirected to this page after the form is submitted */
/* Example: $thank_you_url = 'http://www.yourwebsite.com/thank_you.html'; */

$thank_you_url = 'http://wethebakery.webs.com/thankyou.html';

/*******************************************************************************
 *	Do not change anything below, unless of course you know very well 
 *	what you are doing :)
*******************************************************************************/

$name = array('Name','name',NULL,NULL);
$email = array('Email','email',NULL,NULL,NULL);
$subject = array('Subject','subject',NULL,NULL);
$message = array('Message','message',NULL,NULL);
$security = array('Security question','security',NULL,NULL,NULL);

$error_message = '';

if (!isset($_POST['submit'])) {

  showForm();

} else { //form submitted

  $error = 0;
  
  if(!empty($_POST['check'])) die("Invalid form access");
  
  if(!empty($_POST['name'])) {
  	$name[2] = clean_var($_POST['name']);
  	if (function_exists('htmlspecialchars')) $name[2] = htmlspecialchars($name[2], ENT_QUOTES);
  }
  else {
    $error = 1;
    $name[3] = 'color:#FF0000;';
  }
  
  if(!empty($_POST['email'])) {
  	$email[2] = clean_var($_POST['email']);
  	if (!validEmail($email[2])) {
  	  $error = 1;
  	  $email[3] = 'color:#FF0000;';
  	  $email[4] = '<strong><span style="color:#FF0000;">Invalid email</span></strong>';
	  }
  }
  else {
    $error = 1;
    $email[3] = 'color:#FF0000;';
  }
  
  if(!empty($_POST['subject'])) {
  	$subject[2] = clean_var($_POST['subject']);
  	if (function_exists('htmlspecialchars')) $subject[2] = htmlspecialchars($subject[2], ENT_QUOTES);  	
  }
  else {
  	$error = 1;
    $subject[3] = 'color:#FF0000;';
  }  

  if(!empty($_POST['message'])) {
  	$message[2] = clean_var($_POST['message']);
  	if (function_exists('htmlspecialchars')) $message[2] = htmlspecialchars($message[2], ENT_QUOTES);
  }
  else {
    $error = 1;
    $message[3] = 'color:#FF0000;';
  }    

  if(empty($_POST['security'])) {
    $error = 1;
    $security[3] = 'color:#FF0000;';
  } else {
  	
    if($question_answers[$_POST['question']] != strtolower(clean_var($_POST['security']))) {
      $error = 1;
      $security[3] = 'color:#FF0000;';   
      $security[4] = '<strong><span style="color:#FF0000;">Wrong answer</span></strong>';
    }
  }

  if ($error == 1) {
    $error_message = '<span style="font-weight:bold;font-size:90%;">Please correct/enter field(s) in red.</span>';

    showForm();

  } else {
  	
  	if (function_exists('htmlspecialchars_decode')) $name[2] = htmlspecialchars_decode($name[2], ENT_QUOTES);
  	if (function_exists('htmlspecialchars_decode')) $subject[2] = htmlspecialchars_decode($subject[2], ENT_QUOTES);
  	if (function_exists('htmlspecialchars_decode')) $message[2] = htmlspecialchars_decode($message[2], ENT_QUOTES);  	
  	
    $body = "$name[0]: $name[2]\r\n";
    $body .= "$email[0]: $email[2]\r\n\r\n";
    $body .= "$message[0]:\r\n$message[2]\r\n";
    
    if (!$from) $from_value = $email[2];
    else $from_value = $from;
    
    $headers = "From: $from_value" . "\r\n"; 
    $headers .= "Reply-To: $email[2]" . "\r\n";
    
    mail($to,"$subject_prefix - $subject[2]", $body, $headers);
    
    if (!$thank_you_url) {
    
      include $header_file;
      echo $GLOBALS['thank_you_message'];
      echo "\n";
      include $footer_file;
	  }
	  else {
	  	header("Location: $thank_you_url");
	  }
       	
  }

} //else submitted



function showForm()

{
global $name, $email, $subject, $message, $security, $question_answers, $header_file, $footer_file, $form_width, $form_background, $form_border_color, $form_border_width, $form_border_style, $cell_padding, $left_col_width, $font_size; 
include $header_file;
$question = array_rand($question_answers);
echo $GLOBALS['error_message'];  
echo <<<EOD

<form method="post" class="cForm">
<table style="width:{$form_width}; background-color:{$form_background}; border:{$form_border_width} {$form_border_style} {$form_border_color}; padding:10px; font-size:{$font_size};" class="contactForm">
<tr>
<td style="width:{$left_col_width}; text-align:left; vertical-align:top; padding:{$cell_padding}; font-weight:bold; {$name[3]}">{$name[0]}</td>
<td style="text-align:left; vertical-align:top; padding:{$cell_padding};"><input type="text" name="{$name[1]}" value="{$name[2]}" /></td>
</tr>
<tr>
<td style="width:{$left_col_width}; text-align:left; vertical-align:top; padding:{$cell_padding}; font-weight:bold; {$email[3]}">{$email[0]}</td>
<td style="text-align:left; vertical-align:top; padding:{$cell_padding};"><input type="text" name="{$email[1]}" value="{$email[2]}" /> {$email[4]}</td>
</tr>
<tr>
<td style="width:{$left_col_width}; text-align:left; vertical-align:top; padding:{$cell_padding}; font-weight:bold; {$subject[3]}">{$subject[0]}</td>
<td style="text-align:left; vertical-align:top; padding:{$cell_padding};"><input type="text" name="{$subject[1]}" value="{$subject[2]}" size="40" /></td>
</tr>
<tr>
<td style="width:{$left_col_width}; text-align:left; vertical-align:top; padding:{$cell_padding}; font-weight:bold; {$message[3]}">{$message[0]}</td>
<td style="text-align:left; vertical-align:top; padding:{$cell_padding};"><textarea name="{$message[1]}" cols="40" rows="6">{$message[2]}</textarea></td>
</tr>
<td style="width:{$left_col_width}; text-align:left; vertical-align:top; padding:{$cell_padding}; font-weight:bold; {$security[3]}">{$question}?</td>
<td style="text-align:left; vertical-align:top; padding:{$cell_padding};"><input type="text" name="{$security[1]}" value="" size="10" /> {$security[4]}
<br /><br />We are sorry but please answer the above question to prove you are a real visitor and not a spam bot.</td>
</tr>
<tr>
<td colspan="2" style="text-align:left; vertical-align:middle; padding:{$cell_padding}; font-size:90%; font-weight:bold;">All fields are required.</td>
</tr>
<tr>
<td colspan="2" style="text-align:left; vertical-align:middle; padding:{$cell_padding};"><input type="submit" name="submit" value="Submit" style="border:1px solid #999;background:#E4E4E4;margin-top:5px;" /></td>
</tr>
</table>
<input type="hidden" name="question" value="{$question}">
<input type="hidden" name="check" value="">
</form>
<div style="width:{$form_width};text-align:right;font-size:80%;">
<a href="http://www.easyphpcontactform.com/" title="PHP Contact Form">PHP Contact Form</a>
</div> 
EOD;

include $footer_file;
}

function clean_var($variable) {
    $variable = strip_tags(stripslashes(trim(rtrim($variable))));
  return $variable;
}

/**
Email validation function. Thanks to http://www.linuxjournal.com/article/9585
*/
function validEmail($email)
{
   $isValid = true;
   $atIndex = strrpos($email, "@");
   if (is_bool($atIndex) && !$atIndex)
   {
      $isValid = false;
   }
   else
   {
      $domain = substr($email, $atIndex+1);
      $local = substr($email, 0, $atIndex);
      $localLen = strlen($local);
      $domainLen = strlen($domain);
      if ($localLen < 1 || $localLen > 64)
      {
         // local part length exceeded
         $isValid = false;
      }
      else if ($domainLen < 1 || $domainLen > 255)
      {
         // domain part length exceeded
         $isValid = false;
      }
      else if ($local[0] == '.' || $local[$localLen-1] == '.')
      {
         // local part starts or ends with '.'
         $isValid = false;
      }
      else if (preg_match('/\\.\\./', $local))
      {
         // local part has two consecutive dots
         $isValid = false;
      }
      else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain))
      {
         // character not valid in domain part
         $isValid = false;
      }
      else if (preg_match('/\\.\\./', $domain))
      {
         // domain part has two consecutive dots
         $isValid = false;
      }
      else if (!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/', str_replace("\\\\","",$local)))
      {
         // character not valid in local part unless 
         // local part is quoted
         if (!preg_match('/^"(\\\\"|[^"])+"$/',
             str_replace("\\\\","",$local)))
         {
            $isValid = false;
         }
      }
      if ($isValid && function_exists('checkdnsrr'))
      {
      	if (!(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A"))) {
         // domain not found in DNS
         $isValid = false;
       }
      }
   }
   return $isValid;
}

?>     
        
		<tr>
			<td width="378">&nbsp;</td>
			<td width="211">&nbsp;</td>
			<td width="211">&nbsp;</td>
		</tr>
		<tr>
			<td width="800" colspan="3" background="Websites/We The Bakery/images/altbg.jpg" height="34">

<p align="center"><font color="#000000"><b>2010 &copy; We The Bakery</b></font></p>

			</td>
		</tr>
	</table>
</div>
<font color="black"><p align="center">Tasar&#305;m : SD 
Stüdyo</p>

<div style="text-align: center; font-size: 0.75em;">Design downloaded from free website templates.</div></font></body>
<!-- Freewebs Sticker -->
<p align="right"><noscript></noscript><!-- --><script type="text/javascript" src="http://www.freewebs.com/p.js"></script><script language="JavaScript" src="http://images.freewebs.com/JS/Freebar/btn_mirrorblack.js">
</script></p>
```
The html shows up just fine, but then the php is just shown as html instead of executing.
Examples here:

http://i222.photobucket.com/albums/dd168/brodie1600/php1.jpg?t=1274596306
http://i222.photobucket.com/albums/dd168/brodie1600/php2.jpg?t=1274596344

I'm not sure if it has something to do with the PHP script itself, or the way I'm combining php and html in the php document (know that I'm saving the documents with the .php extension).

If anyone has any clue how I can get that to turn into this: http://www.easyphpcontactform.com/demo/noncaptchaform.php please reply as to how.

Thanks =)


----------



## karlosio (Mar 25, 2010)

This seems to be quite an old script where a lot of global variables have been used. PHP normally has register_globals turned off by default so I'm assuming thats what might be causing it to not execute. You can turn it on by enabling it on your php.ini file.

You can check to see if its turned off by creating a black new file and enter the following into it

<?php php_info(); ?>

name the file phpinfo.php and upload it to your server, browse to it and locate register_globals. Remember to delete this file from the server afterwards.

If the server is provided by a host you could ask them to enable register_globals but im not sure if they will as its a security risk.


----------



## brodie1600 (Aug 19, 2008)

I created a new PHP file in Dreamweaver, and here's what's in the file that was uploaded:


```
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<?php
php_info();
?>
<body>
</body>
</html>
```
Saved as phpinfo.php

And it just came up with text like html would, that's all that was on the page.

I'm using http://www.webs.com/ to host my website, and I just upload the files to there. Would it have to do something with webs.com not allowing php scripts to run? If so, I'd be able to host the website right off my computer. Would that change anything?


----------



## karlosio (Mar 25, 2010)

Yeah it seems (according to their FAQ) http://members.webs.com/pages/faqs/ that they do not allow PHP scripts to be run.



> *What programming languages can I use?*
> 
> Premium service customers can use CGI/Perl. However, this type of programming is not available for free account holders, nor do we support the use of MySQL, MSACCESS or PHP. You can, however, link to remote scripts from your website.


http://www.000webhost.com offers PHP and MYSQL hosting for free (with no ads) if you wish to give them a try. :up:


----------

