Attend XDA's Second Annual Developer Conference, XDA:DevCon 2014!
5,811,541 Members 53,880 Now Online
XDA Developers Android and Mobile Development Forum

[ PROGRAMMING ] PHP - Secure email [ TUTOR ] PHP

Tip us?
 
San.phplogin
Old
(Last edited by San.phplogin; 25th June 2014 at 05:46 PM.) Reason: Caps editing
#1  
San.phplogin's Avatar
Member - OP
Thanks Meter 34
Posts: 33
Join Date: Jun 2014
Location: ▇ ▉ ctOS ▉ ▇
Prompt [ PROGRAMMING ] PHP - Secure email [ TUTOR ] PHP

Hi guys ... ,
Here PHP ..... In our daily life



What does php mean?

php is a self-referentially acronym for PHP: Hypertext Preprocessor. Original it supposedly meant personal home page. It is an open source, server-side, HTML embedded scripting language used to create dynamic Web pages. In an HTML document, PHP script (similar syntax to that of Perl or C ) is enclosed within special PHP tags. Because PHP is embedded within tags, the author can jump between HTML and PHP (similar to ASP and Cold Fusion) instead of having to rely on heavy amounts of code to output HTML. And, because PHP is executed on the server, the client cannot view the PHP code. PHP can perform any task that any CGI program can do, but its strength lies in its compatibility with many types of databases. Also, PHP can talk across networks using IMAP, SNMP, NNTP, POP3, or HTTP. PHP was created sometime in 1994 by Rasmus Lerdorf. During mid 1997, PHP development entered the hands of other contributors. Two of them, Zeev Suraski and Andi Gutmans, rewrote the parser from scratch to create PHP version 3 (PHP3).

In simple words >>>

PHP is a scripting language which is highly powerful open source that's widely used for web development activities to create efficient and dynamic web pages. PHP programming is specialty of Arth InfoSoft, we have developed various industry based websites like realtors, financial, engineering, social networking, hotel and hospitality, B2B, Shopping and many more.




No let's learn [ PHP Login script ] :

Learn to create a simple login system with php + mysql script, this tutorial is easy to follow, teach you step by step.

Quote:
Overview

In this tutorial, we create 3 php files for testing our code.
1. main_login.php
2. checklogin.php
3. login_success.php

Steps
1. Create table "members" in database "test".
2. Create file main_login.php.
3. Create file checklogin.php.
4. Create file login_success.php.
5. Create file logout.php

Quote:
STEP1: Create table "members"
For testing this code, we need to create database "test" and create table "members".




CREATE TABLE `members` (
`id` int(4) NOT NULL auto_increment,
`username` varchar(65) NOT NULL default '',
`password` varchar(65) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=2 ;

--
-- Dumping data for table `members`
--

INSERT INTO `members` VALUES (1, 'john', '1234');

Quote:
STEP2: Create file main_login.php

The first file we need to create is "main_login.php" which is a login form





############### Code

<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="checklogin.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Member Login </strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="mypassword" type="text" id="mypassword"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>

Quote:
STEP3: Create file checklogin.php

We have a login form in step 2, when a user submit their username and password, PHP code in checklogin.php will check that this user exist in our database or not.
If user has the right username and password, then the code will register username and password in the session and redirect to "login_success.php". If username or password is wrong the system will show "Wrong Username or Password".

############### Code

<?php

$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="members"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){

// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>


Quote:
STEP4: Create file login_success.php

User can't view this page if the session is not registered.

############### Code



// Check if session is not registered, redirect back to main page.
// Put this code in first line of web page.


<?php
session_start();
if(!session_is_registered(myusername)){
header("location:main_login.php");
}
?>

<html>
<body>
Login Successful
</body>
</html>

Quote:
STEP5: Create file Logout.php

If you want to logout, create this file. The code in this file will destroy the

// Put this code in first line of web page.


<?php
session_start();
session_destroy();
?>

Quote:
For PHP5 User - checklogin.php

############### Code


<?php

ob_start();
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="members"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Define $myusername and $mypassword
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){

// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}

ob_end_flush();
?>


Quote:
Encrypting Password - Make your Login More Secure :



This is the most secure form of viewing email address < or > logging in to some website .... >>>>>>

SO ... This is what i kept my name >>>> Be secure



Hit thanks button >>> If i helped you in someway
Attached Thumbnails
Click image for larger version

Name:	db_members.gif
Views:	675
Size:	1.8 KB
ID:	2817108   Click image for larger version

Name:	encrypt_password.gif
Views:	670
Size:	11.1 KB
ID:	2817109   Click image for larger version

Name:	member_login.gif
Views:	674
Size:	2.8 KB
ID:	2817110   Click image for larger version

Name:	banner.png
Views:	689
Size:	18.4 KB
ID:	2817138   Click image for larger version

Name:	851557_535801936465660_169463870_n.png
Views:	682
Size:	124.4 KB
ID:	2817139  

Click image for larger version

Name:	android-server-how-to-sf.jpg
Views:	676
Size:	42.8 KB
ID:	2817140  
The Following 2 Users Say Thank You to San.phplogin For This Useful Post: [ Click to Expand ]
 
Hvedrung
Old
#2  
Hvedrung's Avatar
Junior Member
Thanks Meter 2
Posts: 8
Join Date: Jul 2013
Location: Brussels
Good tuto.
I just have one question : Why not use PDO ? Since the goal is security, won't it be more efficient ?
The Following User Says Thank You to Hvedrung For This Useful Post: [ Click to Expand ]
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes