php code reuse

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lmhart
Forum Newbie
Posts: 15
Joined: Tue Apr 14, 2009 1:05 pm

php code reuse

Post by lmhart »

Hi all. I am just now learning php and what it can do for a web page.

I was following some tutorials and got to a section on securing emails. They gave example script: see below

What I am wondering is if I write this code in a php file called "myemail.php" with the code below and then put in

Code: Select all

 
<html>
<body>
        <?php include ("myemail.php"); ?> 
 
</body>
</html>
 
Would it work?



Sample code:

Code: Select all

 
<html>
<body>
<?php
function spamcheck($field)
  {
  //filter_var() sanitizes the e-mail 
  //address using FILTER_SANITIZE_EMAIL
  $field=filter_var($field, FILTER_SANITIZE_EMAIL);
  
  //filter_var() validates the e-mail
  //address using FILTER_VALIDATE_EMAIL
  if(filter_var($field, FILTER_VALIDATE_EMAIL))
    {
    return TRUE;
    }
  else
    {
    return FALSE;
    }
  }
 
if (isset($_REQUEST['email']))
  {//if "email" is filled out, proceed
 
  //check if the email address is invalid
  $mailcheck = spamcheck($_REQUEST['email']);
  if ($mailcheck==FALSE)
    {
    echo "Invalid input";
    }
  else
    {//send email
    $email = $_REQUEST['email'] ; 
    $subject = $_REQUEST['subject'] ;
    $message = $_REQUEST['message'] ;
    mail("someone@example.com", "Subject: $subject",
    $message, "From: $email" );
    echo "Thank you for using our mail form";
    }
  }
else
  {//if "email" is not filled out, display the form
  echo "<form method='post' action='mailform.php'>
  Email: <input name='email' type='text' /><br />
  Subject: <input name='subject' type='text' /><br />
  Message:<br />
  <textarea name='message' rows='15' cols='40'>
  </textarea><br />
  <input type='submit' />
  </form>";
  }
?>
 
</body>
</html>
 
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: php code reuse

Post by califdon »

The fastest way to answer a question like that is to try it.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: php code reuse

Post by McInfo »

califdon wrote:The fastest way to answer a question like that is to try it.
I agree. The best way to learn is through experimentation.

@lmhart: When you say
lmhart wrote:this code in a php file called "myemail.php"
are you referring to the code immediately after that sentence or the code labeled "Sample code:"?

Edit: This post was recovered from search engine cache.
Post Reply