Friday, 4 March 2016

How to create Login Logout functionality with session ?


How to create Login Logout functionality with session ?

In Few Basic Steps we can create Login-Logout with session




Step 1 :   
   
    Load Libraries in autoload.php        //Location :    application/config/autoload.php


    $autoload['libraries'] = array('database', 'email', 'session');

   



Step 2 :   
   
    Load Drivers in autoload.php        //Location :    application/config/autoload.php
   
    $autoload['drivers'] = array('session');


  
   
Step 3 :   
   
    Load Helper Libraries in autoload.php        //Location :    application/config/autoload.php
   
    $autoload['helper'] = array('url', 'file');



Now Comes Logical part We will create Two Controller files & Two View Files One for Before Login & Another for After Login Functionalities

Our Two controller will "Account" and "User" and views will "login_view" and "dashboard_view"




Step 4 :   

    First Create view using simple form on login_view page
   
    Form Code:
   
    <form method="post" action="<?php echo site_url('account/login');?>">
        <table>
            <tr>
                <td><input type="text" name="email" value="" placeholder="E-mail"/></td>
            </tr>
            <tr>
                <td><input type="password" name="password" value="" placeholder="*****" /></td>
            </tr>
            <tr>
                <td><input type="submit" name="login" value="Login" /></td>
            </tr>
        </table>
    </form>





Step 5 :   

   
    Add login logic on Account Controller Page We will use simple & static you can use dynamic using database
   
    function login()
    {
        $email=$this->input->post('email');
        $password=$this->input->post('password');
        if($email=='test@gmail.com' && $password=='123456')
        {
            $session_data=array(
                                'id'=>1,
                                'email'=>$email,
                                'islogin'=>True,
                            );
//            $this->session->set_data($session_data);  //if this line show error use

            $this->session->set_userdata($session_data);

            redirect('user');
        }
    }



   
Step 6:

    On User Controller create the constructor function so that it will called before calling any function
   
   public function __contruct()
    {
        parent::__contruct();
        if((!$this->session->has_userdata('id'))||(!$this->session->has_userdata('email'))||(!$this->session->has_userdata('islogin')))
        {
            redirect('account/login');
        }
    }







Step 7:


    Create a function on User controller to show welcome page simply load welcome page
   
    function dashboard()
    {
        $this->load->view('dashboard');
    }



 
Step 8:
   
    Create dashboard view to show welcome message and user email id
   
    <a href="<?php echo site_url('account/logout');?>">Logout</a>
   
    <h3>Hello <?php echo $this->session->userdata('email');?><h3>

    

Step 9:


    Create Logout functionality on Account controller page
   
    function logout()
    {
        $this->session->sess_destroy();
        redirect('account/login');
    }

Wednesday, 27 January 2016

How to send mail with attachment using php mail function

Html :


<form method="post"  action="" enctype="multipart/form-data">
        <table>
            <tr>
                <td>Full Name :</td>
                <td><input type="text" name="fullname" /></td>
            </tr>
            <tr>
                <td>Email :</td>
                <td><input type="text" name="email" required=""/></td>
            </tr>
            <tr>
                <td>Contactno :</td>
                <td><input type="text" name="contactno" maxlength="13" /></td>
            </tr>
            <tr>
                <td>Subject :</td>
                <td><input type="text" name="subject" required="" /></td>
            </tr>
            <tr>
                <td>Message :</td>
                <td><textarea name="message"  required="" style="height: 80px; resize: none; width: 235px;" ></textarea></td>
            </tr>
            <tr>
                <td>Attachment  :</td>
                <td><input type="file" name="upload"  required="" /></td>
            </tr>
            <tr>
                <td colspan="2"><input type="submit" name="send" value="Send" /></td>
            </tr>
        </table>
</form>


Php : 
 

<?php
    if(isset($_POST['send']))
    {
        $fullname=$_POST['fullname'];
        $email=$_POST['email'];
        $contactno=$_POST['contactno'];
        $subject=$_POST['subject'];
        $message=$_POST['message'];

        /*    Code for upload file    */
        $upload_dir="uploads/attachment/";
        if (!is_dir($upload_dir))
        {
            mkdir($upload_dir, 0777, true);
        }
        $filename=$_FILES["upload"]["name"];
        move_uploaded_file($_FILES["upload"]["tmp_name"], $upload_dir . $filename);
      
        /*    Code for mail    */
      
        $from = $email;
        $to='bharat.prajapat@newtechfusion.com';
        $mail_msg='<table>
            <tr>
                <td>Full Name :</td>
                <td>'.$fullname.'</td>
            </tr>
            <tr>
                <td>Email :</td>
                <td>'.$email.'</td>
            </tr>
            <tr>
                <td>Contactno :</td>
                <td>'.$contactno.'</td>
            </tr>
            <tr>
                <td>Subject :</td>
                <td>'.$subject.'</td>
            </tr>
            <tr>
                <td>Message :</td>
                <td>'.$message.'</td>
            </tr>
        </table>';

        $file = file_get_contents($upload_dir.$filename);

        $content = chunk_split(base64_encode($file));

        $uid = md5(uniqid(time()));
        $replyto='youremailid@gmail.com';

      
        // header
        $header = "From: ".$fullname." <".$email.">\r\n";
        $header .= "Reply-To: ".$replyto."\r\n";
        $header .= "MIME-Version: 1.0\r\n";
        $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";

        // message & attachment
        $nmessage = "--".$uid."\r\n";
        $nmessage .= "Content-type:text/html; charset=iso-8859-1\r\n";
        $nmessage .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
        $nmessage .= $mail_msg."\r\n\r\n";
        $nmessage .= "--".$uid."\r\n";
        $nmessage .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n";
        $nmessage .= "Content-Transfer-Encoding: base64\r\n";
        $nmessage .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
        $nmessage .= $content."\r\n\r\n";
        $nmessage .= "--".$uid."--";
      
        $isSent=mail($to, $subject, $nmessage, $header);
        if($isSent)
        {
            echo "<p style='color:green'>Mail Sent Successfuly Check your Inbox or Spam</p>";
        }
        else
        {
            echo "<p style='color:red'>Mail Sent Failed</p>";
        }
    }
?>