Friday, December 1, 2017

Captcha

How to Create Captcha using PHP


In this tutorial we will discuss How to Create Captcha Using PHP. Cahtcha which stands for Completely Automated Public Turing test to tell Computers and Humans Apart it's the endorsement of the test word entered from a sign-up form such as signing up for Gmail, Yahoo, Facebook, twitter, and else. While understanding other Captcha is according to English Wikipedia
In this tutorial we will create Captcha manually with PHP.

Captcha is actually easy to create because it only creates a function to generate random characters and then put it in the image. And save it in the form of a session or a particular variable.

But Captcha technology is not perfect yet, because Spammers could have cached the Captcha manually, and can also make software to solve Captcha with a certain degree of difficulty.

However, Captcha remains important for the website. Because it plays a role in reducing spam and incorrect form entries.
1.  Creating a Web Captcha Page
Open the text editor, in this tutorial using notepad ++, after that please type or directly copy the code / script below and save it with the name index.html.
<html>
<head>
<title>
Login Page
</ title>
</ head>
<body>
<fill in Your Username and Password </ b> </ p>
<! - Create Login Form ->
<! - specify an action if the form has been submitted ->
<form action = "hasil.php" method = "post">
<table border = "0" cellpadding = "0" cellspacing = "0" align = "center">
<tr>
<td> Username </ td>
<td> <input name = "username" value = "" maxlength = "10"> </ td>
</ tr>
<tr>
<td> Password </ td>
<td> <input type = "password" name = "password" value = "" maxlength = "10"> </ td>
</ tr>
<tr>
<td> Captcha </ td>
<! - we specify the location of the script generate the image ->
<td> <img src = "images.php" alt = "picture" /> </ td>
</ tr>
<td> Fill in the captcha </ td>
<td> <input name = "valueCaptcha" value = "" maxlength = "6" /> </ td>
<tr>
<td> <input type = "submit" value = "Submit"> </ td>
</ tr>
</ table>
</ form>
</ body>
</ html>
2.  Creating Captcha Generate Scripts
Create a new file in your text editor and name it image.php. image.php file must be a folder with index.html file. Here's the script or code in the Captcha generate.
<? php
// enable session
session_start ();
header ("Content-type: image / png");

// name the session named Captcha
$ _SESSION ["Captcha"] = "";

// specify the image size
$ gbr = imagecreate (200, 50);

// background image color
imagecolorallocate ($ gbr, 167, 218, 239);
$ red = imagecolorallocate ($ gbr, 128, 128, 128);
$ black = imagecolorallocate ($ gbr, 0,0,0);

// specify a font
$ font = "monaco.ttf";

// creates a random number and is displayed in the image
for ($ i = 0; $ i <= 5; $ i ++) {

// number of characters
$ number = rand (0, 9);
$ _SESSION ["Captcha"]. = $ Number;
$ angle = rand (-25, 25);
imagettftext ($ gbr, 20, $ angle, 8 + 15 * $ i, 25, $ black, $ font, $ number);

// shadow effect
imagettftext ($ gbr, 20, $ angle, 9 + 15 * $ i, 26, $ red, $ font, $ number);
}

// to create an image
imagepng ($ gbr);
imagedestroy ($ gbr);
?>
Page result of the website after we finish Creating Captcha Generate Scripts.




Thursday, November 30, 2017

Login Form

How to Create Login Form


Login Form
1. Create MySQL Users / Users Table
To create a database and table for example database name is "cms" and table name is "user",then run this code in your MySQL software, you can use phpMyAdmin.
    fieldname: user_id
    type: int (4) auto increment set as primary key

    fieldname: username
    type: varchar (20)

    fieldname: password
    type: varchar (50)

    if successful then your table will be like in the picture below.
Then insert to user table with data like below (make sure when enter password select dropdown function select MD5)


    username: dodo
    password: passwordnyadod0

    username: tri
    password: password1

    Now display your data in user table like picture below.
2. Create Script Code Login Form
For php files we need some files :
    1. index.php
    2. login.php
    3. logout.php
    4. database.php
Index.php will act as the dashboard page, while the database.php page is the page that contains the connect function to the database we created earlier
index.php
<? php
        session_start ();
        $ username = $ _SESSION ['username'];
        $ isLoggedIn = $ _SESSION ['isLoggedIn'];
        
        if ($ isLoggedIn! = '1') {
            session_destroy ();
            header ('Location: login.php');
        }
        ?>
        
        Welcome <? Php echo $ username; ;?>
        <a href="logout.php"> Logout </a>;
        ?>
 login.php
 <? php
        session_start ();
        include 'database.php';
       
        if (! empty ($ _ POST)) {
           
            $ username = $ _POST ['username'];
            $ password = md5 ($ _ POST ['password']);
       
            $ sql = "select * from user where username = '". $ username. "' and password = '". $ password. "'";
            #echo $ sql. "<br />";
            $ query = mysql_query ($ sql) or die (mysql_error ());
       
            // check for valid query
            if ($ query) {
                $ row = mysql_num_rows ($ query);
               
                // if $ row> 0 or username and password are found
                if ($ row> 0) {
                    $ _SESSION ['isLoggedIn'] = 1;
                    $ _SESSION ['username'] = $ username;
                    header ('Location: index.php');
                } else {
                    echo "wrong username or password";
                }
            }
        }
        ?>
       
        <form action = "" method = "post">
        <input type = "text" name = "username">
        <input type = "password" name = "password">
        <input type = "submit" value = "login">
        </ form>
 logout.php
<?php
    session_start();
    session_destroy();
    header('Location: login.php');
    ?>
database.php
?php
$host = "localhost";
$user = "root";
$pass = "root";
$db_name = "cms";

mysql_connect($host, $user, $pass) or die (mysql_error());
mysql_select_db($db_name) or die (mysql_error());
?>
3. Save this files in a folder called "login-form" and place the folder in htdocs folder.
     The htdocs folder is located in the XAMPP folder, in this tutorial we use XAMPP as the web server.

After doing all the steps how to make login form above, now it's time we to try login form with PHP and MySQL which completed session by typing address "http: //localhost/login-form/index.php" on browser page.

Similarly tutorial how to create login form, if there is step less understood, please send question through comment field below. Thanks.




Captcha

How to Create Captcha using PHP In this tutorial we will discuss How to Create Captcha Using PHP. Cahtcha which stands for Completely Aut...