Scripts

Scripts

Okay, here are my scripts. I’ll have a lot more coming later as I write more tutorials. Enjoy these! Oh and don’t forget to include the header if by off-chance, you’ll use one of these.

PHP Error-Reporting


I want to stress that the logging capability of this script is just for fun. Use the error_log() function instead.

<?php
/* PHP script written by Alexander Janus
contact: alexander@davepcguy.com
blog: http://www.davepcguy.com
if you want to use this script, include this header
*/
 set_error_handler('error');

function error($type, $msg, $file, $line, $context) {
echo "<h1>Error!</h1>";
$date = date('l jS \of F Y h:i:s A');
echo "Here is the information provided by the script:";
echo "<hr><pre>";
//shows the error code
echo "Error code: $type<br />";
//error message
echo "Error message: $msg<br />";
//error in what file and what line
echo "Script name and line number of error: $file:$line<br />";
$variable_state = array_pop($context);
echo "Variable state when error occurred: ";
print_r($variable_state);
echo "</pre><hr><br>";

//logging the error
$dir = 'errors.txt ' or die ('Could not be found');
$open = fopen($dir,'a') or die ('Could not be opened');

fwrite($open, "

ERROR: $date
type: $type,
msg: $msg,
file: $file,
line: $line,
context: $context"

) or die ('Could not be written');

die ('Error has been logged<br>');

}

?>


PHP Poem


Execute it and see what happens :)

<?php
/* PHP script written by Alexander Janus
contact: alexander@davepcguy.com
blog: http://www.davepcguy.com
if you want to use this script, include this header
*/
$happiness = "happy, and joyful about your life";
$sadness = "sad, and just plain down";
$loneliness = "lonely because no one is around";
$togetherness = "companionship because you love your friends";
$love = "love, pure love.";
$depression = "depressed, because that happens sometimes, too";
$feelings= array( $love, $depression, $sadness, $happiness, $loneliness, $togetherness);

if (!isset($_POST['submit'])){
?>
Are you feeling:<br />
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<?php
foreach ($feelings as $feel) {
 print "<input type=\"checkbox\" name=\"whatifeel[]\" value=\"$feel\">$feel<br>";
 }
?>
<input type="submit" name="submit" value="Select" />
<?php
}
else {

 foreach (($_POST['whatifeel']) as $f) {
 echo "I'll tell you what is happening, <br> and how you feel, <br>and what is real<br><ul>";
 switch ($f) {

 case $happiness:
 echo "<li>And when you envision yourself<br>in love, with money and wealth<br>you'll feel happy.<br><br>";
 break;
 case $loneliness:
 echo "<li>And when you remember him or her, <br>he or she does not prefer <br>your company nor you.<br><br>";
 break;
 case $sadness;
 echo "<li>And when you think that person or event<br>the memory of sight and scent<br>you remember your sadness.<br><br>";
 break;
 case $togetherness;
 echo "<li>And when you look around and see <br>you and your present company<br>you'll be glad to be.<br><br>";
 break;
 case $love;
 echo "<li>And when you see that one,<br>she will become, <br>and represent your love.<br><br>";
 break;
 case $depression;
 echo "<li>And when you feel on the inside<br>and you know you've tried<br>you feel sad and depressed.<br><br>";
 break;
 }

 }

}?> 


Custom E-mail Form


Here’s a custom e-mail form I created earlier.


<?php
//functions
/* PHP script written by Alexander Janus
contact: alexander@davepcguy.com
blog: http://www.davepcguy.com
if you want to use this script, include this header
*/
function isEmailAddress($value) {
 return eregi('^([a-z0-9])+([\.a-z0-9_-])*@([a-z0-9_-])+(\.[a-z0-9_-]+)*\.([a-z]{2,6})$', $value);
 }
// check whether input is alphabetic
function isAlpha($value) {
 return preg_match('/^[a-zA-Z]+$/', $value);
 }

function name ($var) {
 if (!isset($var)){
 return TRUE;
 }
 if (empty($var)){
 return TRUE;
 }
 if (trim($var) == ''){
 return TRUE;
 }
 if (isAlpha($var) == 0){
 return TRUE;
 }
}
function email ($var) {
 if (!isset($var)){
 return TRUE;
 }
 if (empty($var)){
 return TRUE;
 }
 if (trim($var) == ''){
 return TRUE;
 }
 if (isEmailAddress($var) == 0){
 return TRUE;
 }
}

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

 ?>
Please enter your name and e-mail address. The name field supports only alphabetic characters (letters).
<form action="<?php $_SERVER['PHP_SELF'];?>" method="post">
Name:<br /> <input type="text" name="name" maxlength="41" /><br />
E-mail address:<br /> <input type="text" name="email" maxlength="40" /><br />
Message (<i>optional</i>):<br /> <textarea name="optmessage" cols="30" rows"10" maxlength"140"></textarea><br />
<input type="submit" name="submit" value="Submit" />
</form>

<?php
}

elseif (name($_POST['name']) || email($_POST['email'])) {
 if (name($_POST['name'])) {
 echo "Please enter a valid name. Periods, commas, or other non-alpha characters are not supported.<br/>";
 }
 if (email($_POST['email'])){
 echo "Please enter a valid e-mail address in this format: account@email.com. <br />";}
?>

<form action="<?php $_SERVER['PHP_SELF'];?>" method="post">
Name:<br /> <input type="text" name="name" maxlength="41" /><br />
E-mail address:<br /> <input type="text" name="email" maxlength="40" /><br />
Message (<i>optional</i>):<br /> <textarea name="optmessage" cols="30" rows"10" maxlength"140"></textarea><br />
<input type="submit" name="submit" value="Submit" />
</form>
 <?php

}
else {
//Database connection
//MySQL variable
$host = "localhost";
$user = "user";
$pass = "password";
$db = "coupon";

//connect
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

// select database
mysql_select_db($db) or die ("Unable to select database!");

//variables

$to = $_POST['email'];
$subject = "Coupon from the Chiropractors";
$name = $_POST['name'];
$optmessage= $_POST['optmessage'];
$opt = wordwrap ($optmessage, 70);
$sent= "<div id=\"printReady\">
<div style=\" width: 457px; background: white; text-align: center;\">
<p> Dear $name,<br />
Thank you for visiting us at <a href=\"http://www.davepcguy.com\">Website</a>
Here is your coupon</p>
<div align=\"center\" style=\"padding=\"50px\">
<span style=\"position: absolute; margin: 75px 120px 10px 50px; font-size: 30px; border-bottom: 2px solid black; width: 250px\"> $name </span>
<img src=\"coupon2.png\" width=\"436\" height=\"292\" /></div>
<p> Please print this coupon out and present it on your next visit. Thank you, and we hope to see you soon!</p>
<p style=\"margin-left: 50px\"> Sincerely, Alexander </p>
<p> P.S. please print out your coupon immediately or save this page because it will not be available after you close out.
If you're having any trouble, please contact our <a href=\"mailto:alexander@davepcguy.com\">webmaster</a>. </p>
<p>Optional Message: <br />
$opt
</p></div></div>
<form id=\"printMe\" name=\"printMe\">  <input type=\"button\" name=\"printMe\" onClick=\"printSpecial()\" value=\"Print this Page\"> </form>
";

//MYSQL QUERY
//check for duplicates
$duplicate = mysql_query("SELECT * FROM coupons where email='$to'");
$affected_rows = mysql_num_rows($duplicate);
if($affected_rows >= 1)
{
echo $sent;
echo "<span style=\"color: red;\">Note: If you have already entered your e-mail address before, you will not get another e-mail message. <br />
This is to prevent spamming.</span>";
die('');
}
else {
// create query
 $query = "INSERT INTO coupons (name, email) VALUES ('$name', '$to')";

 // execute query
 $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

 // close connection
 mysql_close($connection);

}

//html message
$message = "<html>
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />
<title>Chiropractic</title>
</head>

<body>
<p> Dear $name,<br />
Thank you for visiting us at our<a href=\"http://www.davepcguy.com\">Website</a>
<p> Please print out your coupon and present it on your next visit. Thank you, and we hope to see you soon!</p>
<p style=\"margin-left: 50px\"> Sincerely, Alexander </p>
<p>optional message:<br />
$opt</p>
<p>
Your e-mail: $to </p>
</body>
</html>";

 //header information
$from = "no-reply@davepcguy.com";
$headers = "From: $from" . "\r\n";
$headers .= 'MIME-Version: 1.5.2' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "BCC: reply@davepcguy.com" . "\r\n";

//functions
$mail_sent= mail("$to", $subject, $message, $headers);

//announcement

echo $mail_sent ? $sent : "Mail failed";
echo $sent;
}

?>

Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • LinkedIn
  • Reddit
  • Tumblr
  • Twitter
  • email
  • Slashdot
  • StumbleUpon
No comments yet.
No trackbacks yet.