Tuesday, May 24, 2016

PHP Tutorial 9

MD5 Encryption
<?php
/*
$string ='password';
$string_hash = md5($string);
echo $string_hash;
*/
if(isset($_POST['user_password']) && !empty($_POST['user_password'])){
            $user_password = md5($_POST['user_password']);
           
            $filename = 'hash.txt';
            $handle = fopen($filename, 'r');
            $file_password=fread($handle, filesize($filename));
           
            if($user_password==$file_password){
                        echo 'Password ok!';
            } else {
                        echo 'Incorrect password.';
            }
} else {
            echo 'Please enter a password';
}
?>
<form action ="some2.php" method="POST">
Password: <input type="text" name="user_password"><br><br>
<input type="submit" value="Submit">
</form>

Sending an Email
<?php
$to='alex@phpacademy.org';
$subject ='This is an email';
$body='This is a test email'."\n\n".'Hope you got it.';
$headers='From: Zoljargal <someone@phpacademy.org>';

if(mail($to, $subject, $body, $headers)){
            echo 'Email has been sent to '.$to;
} else {
            echo 'There was an error sending the email.';
}
?>

Creating a Simple Contact Form
<?php
if(isset($_POST['contact_name']) && isset($_POST['contact_email']) && isset($_POST['contact_text'])){
            $c_name = $_POST['contact_name'];
            $c_email = $_POST['contact_email'];
            $c_text = $_POST['contact_text'];
            if(!empty($c_name) && !empty($c_email) && !empty($c_text)){
                        if(strlen($c_name)>25 || strlen($c_email)>50 || strlen($c_text)>1000){
                                    echo 'Sorry, maxLength for each field has been exceeded.';
                        } else {
                                                $to='ub_edu@yahoo.com';
                                                $subject ='Contact form submitted.';
                                                $body=$c_name."\n\n".$c_text;
                                                $headers='From: '.$c_email;

                                                if(mail($to, $subject, $body, $headers)){
                                                            echo 'Email has been sent to '.$to;
                                                } else {
                                                            echo 'There was an error sending the email.';
                                                }
                                    }
            } else {
                        echo 'All fields are required.';
            }
}
?>
<form action = "some2.php" method="POST">
Name:<br><input type="text" name="contact_name" maxlength="25"><br><br>
Email address:<br><input type="text" name="contact_email" maxlength="50"><br><br>
Message:<br>
<textarea  name="contact_text" rows="6" cols="30" maxlength="1000"></textarea><br><br>
<input type="submit" value="Submit">
</form>

Reading a Simple XML File
<?php
$xml = simplexml_load_file('names.xml');
// echo $xml->producer[1]->name.' is '.$xml->producer[1]->age;
foreach($xml->producer as $producer){
echo $producer->name.'('.$producer->age.')<br>';
foreach($producer->show as $show){
echo $show->showname.' on '.$show->showdate.'<br>';
}
}

?>

No comments:

Post a Comment