Monday, March 23, 2009

Log in Script Using PHP

Overview

In this tutorial create 3 files
1. main_login.php
2. checklogin.php
3. login_success.php

Step
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

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 ;



Create file main_login.php




























Member Login
Username:
Password:



Create file checklogin.php



Create file login_success.php

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




Login Successful



Logout.php

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


For PHP5 User - checklogin.php

// 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();
?>



Log In script Using Perl

Log In script Using Perl

Login.pl

#!/www/perl/bin/perl.exe
print "Content-type: text/html\n\n";
use CGI;
use CGI::Carp qw/fatalsToBrowser warningsToBrowser/;
use CGI::Session ( '-ip_match' );

$q = new CGI;

$usr = $q->param('usr');
$pwd = $q->param('pwd');

if($usr ne '')
{
# process the form
if($usr eq "demo" and $pwd eq "demo")
{
$session = new CGI::Session();
print $session->header(-location=>'index.pl');

}
else
{
print $q->header(-type=>"text/html",-location=>"login.pl");
}
}
elsif($q->param('action') eq 'logout')
{
$session = CGI::Session->load() or die CGI::Session->errstr;
$session->delete();
print $session->header(-location=>'login.pl');
}
else
{
print $q->header;
print <

Username:

Password:




HTML
}



Index.pl

#!/www/perl/bin/perl.exe
print "Content-type: text/html\n\n";
# index.pl
use CGI;
use CGI::Carp qw/fatalsToBrowser warningsToBrowser/;
use CGI::Session ( '-ip_match' );

$session = CGI::Session->load();
$q = new CGI;

if($session->is_expired)
{
print $q->header(-cache_control=>"no-cache, no-store, must-revalidate");
print "Your has session expired. Please login again.";
print "
login";
}
else
{
print $q->header(-cache_control=>"no-cache, no-store, must-revalidate");
print "

Welcome";
print "
Logout";
}

Thursday, March 19, 2009

My Favorite Tutorial for Perl



My Favorite Tutorial for Perl

Part Time Jobs


Earn Rs.2000 daily. No Investment.

Wanted Online Internet job workers. Job is only through Internet. Work part time. You can earn Rs.750-2000/- daily. These are genuine Internet jobs. No Investment required. Only serious enquires please. For more details visit http://www.earnparttimejobs.com/index.php?id=349564

Followers