Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Thursday, December 16, 2010

Send E-Mail

<?php
function spamcheck($field)  {
   $field=filter_var($field, FILTER_SANITIZE_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("bala@gmail.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='secureMail.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>";
  }
?>

Upload File

<?php
if ((($_FILES["file2"]["type"] == "image/gif")
|| ($_FILES["file2"]["type"] == "image/jpeg")
|| ($_FILES["file2"]["type"] == "image/pjpeg"))
&& ($_FILES["file2"]["size"] < 20000))
  {
  if ($_FILES["file2"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file2"]["name"] . "<br />";
    echo "Type: " . $_FILES["file2"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file2"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file2"]["tmp_name"] . "<br />";


    if (file_exists("upload/" . $_FILES["file2"]["name"]))
      {
      echo $_FILES["file2"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file2"]["tmp_name"],
      "upload/" . $_FILES["file2"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file2"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?>

Session

<?php
// STARTING A SESSION
session_start();

// STORING A SESSION
if(isset($_SESSION['views'])) {
    $_SESSION['views']=$_SESSION['views']+1;
    $_SESSION['age']=20;
    $_SESSION['name']="Nathan";
}
else {
    $_SESSION['views']=1;
    $_SESSION['age']=23;
    $_SESSION['name']="Nanthan";
}

// RETRIEVING A SESSION
echo "Views=". $_SESSION['views'] . "<br>";
echo "Name=". $_SESSION['name'] . "<br>";
echo "Ages=". $_SESSION['age'] . "<br>";

// DESTROYING A SESSION
session_destroy();
?>

Cookie

Creating a cookie
<?php
$expire=time()+60*60*24*30;
setcookie("user", "Alex Porter", $expire);
?>

Handling a cookie
<?php
    if (isset($_COOKIE["user"]))
echo "Welcome " . $_COOKIE["user"] . "!<br />";
    else
        echo "Welcome guest!<br />";
?>

Deleting a cookie
<?php
    setcookie("user", "", time()-$expire); //setting a past time
?>