Saturday, 30 November 2013

Login Code in PHP

In this tutorial, we create 3 php files for testing our code.

1. index.php
2. logincheck.php
3. success.php

4 Steps to create login page

1. Create table "userlist" in database "demo".
2. Create file index.php.
3. Create file logincheck.php.
4. Create file success.php.

Create file index.php.


<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form" method="post" action="logincheck.php">
<td>
<table width="100%" border="0" cellpadding="2" cellspacing="2" bgcolor="#000">
<tr>
<td colspan="3"><strong>Login Page</strong></td>
</tr>
<tr>
<td width="78">Userid / Email id</td>
<td width="6">:</td>
<td width="294"><input name="txtusername" type="text" id="txtusername"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="txtpassword" type="text" id="txtpassword"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>

Creating database with table and data


CREATE TABLE `userlist` (
`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 ;

INSERT INTO `userlist` VALUES (1, 'admin', 'admin123');


Create logincheck.php page

<?php

$host="localhost"; // Host name
$username=""; // username
$password=""; // password
$dbname="demo"; // Database name
$tblname="userlist"; // Table name

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

// username and password sent from form
$txtusername=$_POST['txtusername'];
$txtusername=$_POST['txtpassword'];

// To protect MySQL injection (more detail about MySQL injection)
$txtusername= stripslashes($txtusername);
$txtusername= stripslashes($txtusername);
$txtusername= mysql_real_escape_string($txtusername);
$txtusername= mysql_real_escape_string($txtusername);
$sql="SELECT * FROM $tblname WHERE username='$txtusername' and password='$txtusername'";
$result=mysql_query($sql);

// Counting table row
$row=mysql_num_rows($result);

// If  matched $txtusernameand $txtusername, table row must be 1 row
if($row==1){

// Register redirect to file "success.php"
session("txtusername");
session("txtusername");
header("location:success.php");
}
else {
echo "Invalid Username or Password";
}
?>

Create Success Page


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

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

PHP database Connection


<?php
$dbhost = 'localhost:1080';
$dbuser = 'username'; // in local root is default username
$dbpass = 'password';
$con = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $con )
{
  die('connection error : ' . mysql_error());
}
echo 'Connected successfully';
$sql = 'CREATE Database test_database';
$query= mysql_query( $sql, $conn );
if(! $query )
{
  die('Not create database: ' . mysql_error());
}
echo "Database created successfully\n";
mysql_close($conn);
?>

Mail Function in PHP

How to send mail in php

Mail Function in PHP


<html>

<body>

<?php

if (isset($_REQUEST['email']))

//if "email" is filled out, send email

  {

  //send email

  $email = $_REQUEST['email'] ;

  $subject = $_REQUEST['subject'] ;

  $message = $_REQUEST['message'] ;

  mail("someone@example.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='mailform.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>";

  }

?>

</body>

</html>


Mail Function in PHP

Image Uploading in PHP

how to upload Image in PHP

File Name save as form.php

<html>
<head>
<script type="text/javascript">
function validate(){
var filevalue=document.getElementById("file").value;
var description=document.getElementById("description").value;
if(filevalue=="" || filevalue.length<1){
alert("Select File.");
document.getElementById("file").focus();
return false;
}
if(description=="" || description.length<1){
alert("File Description must not be blank.");
document.getElementById("description").focus();
return false;
}

return true;
}
</script>
</head>
<body >
<h2 align="center" >File Upload</h2>
<form action="file_upload.php" method="post" enctype="multipart/form-data" onSubmit="return validate()" >
<table align="center" >
<tr>
<td><label for="file">File:</label></td>
<td><input type="file" name="file" id="file" /></td>
</tr>
<tr>
<td><label >File Description:</label></td>
<td><input type="text" name="description" id="description" /></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="Submit" /></td>
</tr>
<table>
</form>
</body>
</html>

Second File Name save as file_upload.php

?php
include("connect.php"); //database connection
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 1000000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "File Error : " . $_FILES["file"]["error"] . "<br />";
}
else {

echo "Upload File Name: " . $_FILES["file"]["name"] . "<br />";
echo "File Type: " . $_FILES["file"]["type"] . "<br />";
echo "File Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "File Description:: ".$_POST['description']."<br />";

if (file_exists("images/".$_FILES["file"]["name"]))
{
echo "<b>".$_FILES["file"]["name"] . " already exists. </b>";
}else
{
move_uploaded_file($_FILES["file"]["tmp_name"],"images/". $_FILES["file"]["name"]);

$loc="images/".$_FILES["file"]["name"];
$qu="insert into images.img(loc) values('$loc')";
mysql_query($qu,$con);
?>
Uploaded File:<br>
<img src="images/<?php echo $_FILES["file"]["name"]; ?>" alt="Image path Invalid" >
<?php
}
}
}else
{
echo "Invalid file detail ::<br> file type ::".$_FILES["file"]["type"]." , file size::: ".$_FILES["file"]["size"];
}
?>

Database


Database name: image
Table name : img
Table fields : imgid(int primary key, auto incr) , loc(varchar)