This is default featured slide 1 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 2 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 3 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 4 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 5 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

Monday 14 May 2012

How to write a php script for file downloading

In this tutorials you will learn how to write a php script for file downloading.If you want to see how to upload a file into a server click here.

<?php 
// Give the path here where your file is located and make a hyper link like <a href='www.yoursite.com?fname=youpic.jpg'>Download image</a>
//$_GET['fname']; will get the file name you given in hyper link and start downloading

$file = 'admin/upload/'.$_GET['fname'];
header("Expires: 0"); 
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); 
header("Cache-Control: no-store, no-cache, must-revalidate"); 
header("Cache-Control: post-check=0, pre-check=0", false); 
header("Pragma: no-cache"); 
header("Content-type: application/pdf");   
header('Content-length: '.filesize($file)); 
header('Content-disposition: attachment; filename='.basename($file)); 
readfile($file); 
// Exit script. So that no useless data is output-ed. 
exit; 
?> 

 

Wednesday 9 May 2012

Upload image into folder and save its name into database

This tutorial tell you how to Upload image into folder and save its name into database.
You should follow these two steps:
  1. Creat a database named 'olympic' and a table named 'acc_images'.
    CREATE TABLE IF NOT EXISTS `acc_images` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `acc_id` int(11) unsigned NOT NULL, `image` varchar(255) NOT NULL, `status` varchar(20) NOT NULL, PRIMARY KEY (`id`), );
  2. Copy and past code given below.
    <?php
    // Assigning value about your server to variables for database connection
    $hostname_connect= "localhost";
    $database_connect= "olympic";
    $username_connect= "root";
    $password_connect= "";
    $connect_solning = mysql_connect($hostname_connect, $username_connect, $password_connect) or trigger_error(mysql_error(),E_USER_ERROR);
    @mysql_select_db($database_connect) or die (mysql_error());

    if($_POST)
    {
    // $_FILES["file"]["error"] is HTTP File Upload variables $_FILES["file"] "file" is the name of input field you have in form tag.

    if ($_FILES["file"]["error"] > 0)
    {
    // if there is error in file uploading
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";

    }
    else
    {
    // check if file already exit in "images" folder.
    if (file_exists("images/" . $_FILES["file"]["name"]))
    {
    echo $_FILES["file"]["name"] . " already exists. ";
    }
    else
    {  //move_uploaded_file function will upload your image.  if you want to resize image before uploading see this link http://b2atutorials.blogspot.com/2013/06/how-to-upload-and-resize-image-for.html
    if(move_uploaded_file($_FILES["file"]["tmp_name"],"images/" . $_FILES["file"]["name"]))
    {
    // If file has uploaded successfully, store its name in data base
    $query_image = "insert into acc_images (image, status, acc_id) values ('".$_FILES['file']['name']."', 'display','')";
    if(mysql_query($query_image))
    {
    echo "Stored in: " . "images/" . $_FILES["file"]["name"];
    }
    else
    {
    echo 'File name not stored in database';
    }
    }
    }


    }
    }
    ?>
    <html>
    <body>
    <form action="" method="post"enctype="multipart/form-data">
    <label for="file">Filename:</label>
    <input type="file" name="file" id="file" />
    <br />
    <input type="submit" name="submit" value="Submit" />
    </form>
    </body>
    </html>

Tuesday 8 May 2012

Create a comma separated csv from sql query


<?php
$select = "SELECT * FROM Your_table";
$export = mysql_query ( $select ) or die ( "Sql error : " . mysql_error( ) );

$fields = mysql_num_fields ( $export );
for ( $i = 0; $i < $fields; $i++ )
{
    $header .= '"'.mysql_field_name( $export , $i ).'"';
 if($i != $fields-1)
 $header .= ",";
}
while( $row = mysql_fetch_row( $export ) )
{

    if(isset($row[0]) && $row[0] != ''){
  $row[0] = text_decrypt($row[0]);
 }
 $line = '';
    foreach( $row as $value )
    {                                           
        if ( ( !isset( $value ) ) || ( $value == "" ) )
        {
            $value = "\t";
        }
        else
        {
            $value = str_replace( '"' , '""' , $value );
            $value = '"' . $value . '"' . ",";
        }
        $line .= $value;
    }

 $line = substr($line, 0, -1);
    $data .= trim( $line ) . "\n";
}
$data = str_replace( "\r" , "" , $data );
if ( $data == "" )
{
    $data = "\n(0) Records Found!\n";                       
}
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=your_file_name.csv");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";
?>

Sunday 6 May 2012

Make a Sign in form and store data in session

This tutorial tell you how to match username and password entered by user with stored records in database. Then store username in session variable. You can see sing up form here
You should follow these two points:
  1. Creat a database named 'olympic' and a table named 'user'.
    CREATE TABLE IF NOT EXISTS `user` ( `username` varchar(255) NOT NULL, `email` varchar(255) NOT NULL, `password` varchar(255) NOT NULL, `postal_code` int(11) NOT NULL, `status` varchar(20) NOT NULL, `key` varchar(20) NOT NULL, `id` int(11) unsigned NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=32 ;
  2. Copy and past code given below.
    <?php
    // Assigning value about your server to variables for database connection
    $hostname_connect= "localhost";
    $database_connect= "olympic";
    $username_connect= "root";
    $password_connect= "";
    $connect_solning = mysql_connect($hostname_connect, $username_connect, $password_connect) or trigger_error(mysql_error(),E_USER_ERROR);
    @mysql_select_db($database_connect) or die (mysql_error());

    if($_POST)
    {
    $username=$_POST['username'];
    $password=$_POST['password'];

    $user_query="select * from user where username='$username'and password='$password'";

    $user_result=mysql_query($user_query);
    $user_row=mysql_fetch_array($user_result);
    $id=$user_row['id'];
    $num_user=mysql_num_rows($user_result);
    // Above query matches username and password entered by user with database records.
    if($num_user==1)
    {
    // if username and password exist in database assign username to session.
    // echo $_SESSION['username'] where you want to display username to user in you website
    $_SESSION['username'] = $user_row['username'];
    $_SESSION['id']=$id;
    // redirect user to home page or where you want
    header('Location: index.php');
    }
    else
    {
    echo "Invalid User Name or Password";
    }
    }
    ?>


    <form id='register' action='' method='post'>
    <fieldset >
    <legend>Register</legend>
    <input type='hidden' name='submitted' id='submitted' value='1'/>
    <label for='username' >Username*: </label><br/>
    <input type='text' name='username' id='username' value="<?php if(!empty($username)) echo $username ?>" maxlength="50" /><br/>
    <label for='Password' >Password*:</label><br/>
    <input type='password' name='password' id='password' maxlength="50" /><br/>
    <input type='submit' name='Submit' value='Submit' />
    </fieldset>
    </form>

How to save form data in database and send activation key

In this tutorial you will learn how to save signup form data in database and send activation key to user's email. I am not including validation code here if you want to validate your form submission code click here to see. For looking the sign in form click here
You should follow these two points:
  1. Creat a database named 'olympic' and a table named 'user'.
    CREATE TABLE IF NOT EXISTS `user` ( `username` varchar(255) NOT NULL, `email` varchar(255) NOT NULL, `password` varchar(255) NOT NULL, `postal_code` int(11) NOT NULL, `status` varchar(20) NOT NULL, `key` varchar(20) NOT NULL, `id` int(11) unsigned NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=32 ;
  2. Copy and past code given below.
    <?php
    // Assing value about your server to these variables for database connection
    $hostname_connect= "localhost";
    $database_connect= "olympic";
    $username_connect= "root";
    $password_connect= "";
    $connect_solning = mysql_connect($hostname_connect, $username_connect, $password_connect) or trigger_error(mysql_error(),E_USER_ERROR);
    @mysql_select_db($database_connect) or die (mysql_error());

    if($_POST)
    {
    $key = mt_rand(); // this mt_rand() function genrates randum key for you
    $username = $_POST['username'];
    $email = $_POST['email'];
    $password = $_POST['password'];
    $passconf = $_POST['passconf'];
    $postal_code = $_POST['postal_code'];

    // code below checks username in database exist or not. if exist data will not submit to database
    $user_query="select username from user where username='$username'";

    $user_result=mysql_query($user_query);
    $user_row=mysql_fetch_array($user_result);
    if(!empty($user_row))
    {
    if($user_row['username'])
    {
    echo 'Username Already exist';
    }


    }
    else
    {
    //If username not already exist then insert data into 'user' table
    $q = "INSERT INTO `user`(`username`,`email`,`password`,`postal_code`,`key`,`status`)VALUES('$username','$email','$password','$postal_code','$key','InActive')";

    $qr=mysql_query($q);
    if(!$qr){
    echo "Not Submitted";
    }
    else
    {
    // if data has saved in data base then code below will send email to user
    $to = $email;
    $subject = "Activation key";
    $message = "Your activation key is this" .$key.'<br>'.' click here to activate your acount.<a href="localhost">here</a>';
    $from = "admin_abc@yahoo.com";
    $headers = "From:" . $from;
    if(mail($to,$subject,$message,$headers))
    {
    echo "Check your email to activate your acount.";
    }
    else
    {
    echo "Email not sended";
    // if you are working on localhost email will not send and below line helps you to activates your acount
    echo $message = "Your activation key is this " .$key.'<br>'.' click here to activate your acount. <a href="activation.php?key='.$key.'">here</a>';
    }
    }
    }
    }
    ?>
    <html>
    <body>
    <form id='registeration' action='' method='post' >
    <fieldset >
    <legend>Register</legend>
    <input type='hidden' name='submitted' id='submitted' value='1'/>
    <label for='username' >Username*: </label><br/>
    <input type='text' name='username' id='username' value="<?php if(!empty($username)) echo $username ?>" maxlength="50" /><br/>
    <label for='email' >Email Address*:</label><br/>
    <input type='text' name='email' id='email' value="<?php if(!empty($email)) echo $email ?>" maxlength="50" /><br/>
    <label for='password' >Password*:</label><br/>
    <input type='password' name='password' id='password' maxlength="50" /><br/>
    <label for='passconf' >Confirm Password*:</label><br/>
    <input type='password' name='passconf' id='password' maxlength="50" /><br/>
    <label for='postal_code' >Postal Code*:</label><br/>
    <input type='text' name='postal_code' id='postal_code' value="<?php if(!empty($postal_code)) echo $postal_code ?>" maxlength="50" /><br/>
    <input type='submit' name='Submit' value='Submit' />
    </fieldset>
    </form>
    </body>
    </html>