PHP, MySQL, Drupal, .htaccess, Robots.txt, Phponwebsites: cookies
cookies - phponwebsites.com
Showing posts with label cookies. Show all posts

13 Jul 2014

Send forgotten password to mail in login form using php and mysql

                       Already you know about validate login form with remember me and signout concepts using php. You want to know Login with remember me and signout using php and mysql
                       Now see about how to send forgotten password to mail using php?. First your table contains users mail id. Just imagine this is your table:


Mysql login table

Login form using php and mysql:

         
          Consider the following example:
login.php:

<html>
<head>
<style type="text/css">
 input{
 border:1px solid olive;
 border-radius:5px;
 }
 h1{
  color:darkgreen;
  font-size:22px;
  text-align:center;
 }
span{
  color:lightgreen;

 }
</style>
</head>
<body>
<h1>Login<h1>
<form action='#' method='post'>
<table cellspacing='5' align='center'>
<tr><td>User name:</td><td><input type='text' name='name'/></td></tr>
<tr><td>Password:</td><td><input type='password' name='pwd'/></td></tr>
<tr><td></td><td><input type='checkbox' name='remember' /> <span>Remember me</span></td></tr>
<tr><td></td><td> <span><a href="forgot.php">Forgot password</a></span></td></tr>
<tr><td></td><td><input type='submit' name='submit' value='Submit'/></td></tr>
</table>

</form>
<?php
session_start();
//your values are stored in cookies, then you can login without validate
if(isset($_COOKIE['name']) && isset($_COOKIE['pwd']))
{
    header('location:welcome.php');
}
// login validation in php
if(isset($_POST['submit']))
{
 mysql_connect('localhost','root','') or die(mysql_error());
 mysql_select_db('new')  or die(mysql_error());
 $name=$_POST['name'];
 $pwd=$_POST['pwd'];
 if($name!=''&&$pwd!='')
 {
   $query=mysql_query("select * from login where name='".$name."' and password='".$pwd."' ")  or die(mysql_error());
   $res=mysql_fetch_row($query);
   if($res)
   {
    if(isset($_POST['remember']))
{
  setcookie('name',$name, time() + (60*60*24*1));
  setcookie('pwd',$pwd, time() + (60*60*24*1));
}
    $_SESSION['name']=$name;
    header('location:welcome.php');
   }
   else
   {
    echo'You entered username or password is incorrect';
   }
 }
 else
 {
  echo'Enter both username and password';
 }
}
?>
</body>
</html>


             While you run the above file, you'll get output like this:


Login form using php

Fogot password in php:


                 The php script for send forgotten password to mail is:
forgot.php:


<html>
<head>
<style type="text/css">
 input{
 border:1px solid olive;
 border-radius:5px;
 }
 h1{
  color:darkgreen;
  font-size:22px;
  text-align:center;
 }

</style>
</head>
<body>
<h1>Forgot Password<h1>
<form action='#' method='post'>
<table cellspacing='5' align='center'>
<tr><td>Email id:</td><td><input type='text' name='mail'/></td></tr>
<tr><td></td><td><input type='submit' name='submit' value='Submit'/></td></tr>
</table>
</form>
<?php
if(isset($_POST['submit']))

 mysql_connect('localhost','root','') or die(mysql_error());
 mysql_select_db('new') or die(mysql_error());
 $mail=$_POST['mail'];
 $q=mysql_query("select * from login where mail='".$mail."' ") or die(mysql_error());
 $p=mysql_affected_rows();
 if($p!=0) 
 {
  $res=mysql_fetch_array($q);
  $to=$res['mail'];
  $subject='Remind password';
  $message='Your password : '.$res['password']; 
  $headers='From:guruparthiban19@gmail.com';
  $m=mail($to,$subject,$message,$headers);
  if($m)
  {
    echo'Check your inbox in mail';
  }
  else
  {
   echo'mail is not send';
  }
 }
 else
 {
  echo'You entered mail id is not present';
 }
}
?>
</body>
</html>

         Where,
                 mysql_query("select * from login where mail='".$mail."' "); is select the values from mysql table.
                 mysql_affected_rows() return the number of rows selected. If it returns 0, then there is no values is present in mysql table.
                 mysql_fetch_array() return the results an associated array. You want to know mysql_fetch_array().
                  Mail() is used to send password to mail. Yow want to know mail() in php.

              When you click the 'forgot password' in login page, you'll get output like this:

Forgot password in php

                You entered your email id in text box and click submit button. Now you can get message 'Check your inbox in mail' if the mail is send. Otherwise, you'll get 'Mail is not send' message. If you enetered mail id which is not present is mysql table, then you will get 'You entered mail is is not present' message.

7 Jul 2014

Validate login form with remember me using php and mysql

                      All of you know the simple login validation concepts in php. Otherwise visit login validation using php. Now see about login form with remember me concept in php. You can remember the user using cookies concept in php.
                     When you click the remember me check box, your name and password is stored in cookie. Though you signout and close the browser, you can login again while open page in browser. Your name kept by cookie until expire the time or you clear the cache in your browser.

Remember me in login form using php:


                     Consider the following example. You have 3 php pages such as login.php, welcome.php and signout.php.

login.php:


<html>
<head>
<style type="text/css">
 input{
 border:1px solid olive;
 border-radius:5px;
 }
 h1{
  color:darkgreen;
  font-size:22px;
  text-align:center;
 }
span{
  color:lightgreen;

 }
</style>
</head>
<body>
<h1>Login<h1>
<form action='#' method='post'>
<table cellspacing='5' align='center'>
<tr><td>User name:</td><td><input type='text' name='name'/></td></tr>
<tr><td>Password:</td><td><input type='password' name='pwd'/></td></tr>
<tr><td></td><td><input type='checkbox' name='remember' /> <span>Remember me</span></td></tr>
<tr><td></td><td><input type='submit' name='submit' value='Submit'/></td></tr>
</table>

</form>
<?php
session_start();
//your values are stored in cookies, then you can login without validate
if(isset($_COOKIE['name']) && isset($_COOKIE['pwd']))
{
    header('location:welcome.php');
}
// login validation in php
if(isset($_POST['submit']))
{
 mysql_connect('localhost','root','') or die(mysql_error());
 mysql_select_db('new') or die(mysql_error());
 $name=$_POST['name'];
 $pwd=$_POST['pwd'];
 if($name!=''&&$pwd!='')
 {
   $query=mysql_query("select * from login where name='".$name."' and password='".$pwd."' ");
   $res=mysql_fetch_row($query);
   if($res)
   {
    if(isset($_POST['remember']))
{
 setcookie('name',$name, time() + (60*60*24*1));
 setcookie('pwd',$pwd, time() + (60*60*24*1));
}
    $_SESSION['name']=$name;
    header('location:welcome.php');
   }
   else
   {
    echo'You entered username or password is incorrect';
   }
 }
 else
 {
  echo'Enter both username and password';
 }
}
?>
</body>
</html>

             where,
            if(isset($_COOKIE['name']) && isset($_COOKIE['pwd'])) {} means if the user values in  cookies, then the user can login directly without login validation in php.
         
           if(isset($_POST['remember'])){
           setcookie('name',$name,time() + (60*60*24*1));
           setcookie('pwd',$pwd,time() + (60*60*24*1));
           }
           which means, if the user click the remember me check box, then you have to store them in cookie. If you don't know  about cookies in php, then visit Cookies in php.

          When you run the login.php fille, you'll get output like this:

validate remember me concept in login form using php

welcome.php:


<?php    
  session_start();
  if(isset($_COOKIE['name']))
  {
    echo 'Welcome: '. $_COOKIE['name'].'<br>';
  }
  else
  {
    echo 'Welcome: '. $_SESSION['name'].'<br>';
  }
  echo'<a href="signout.php">Singout</a>';
?>

      where,
                  if(isset($_COOKIE['name'])){}else{} means if the values in cookie, then return name from cookie. Otherwise name return from session.
             Now you enter username and password  'guru' and 'guru' without select the remember check box, you will navigate to 'welcome.php' page and get output like this:

                   welcome: guru
                   Signout

 If it is wrong, then you can't login.

   

Signout in php:           


            When you click the signout.php, it destroy the session and return back to login page. The php script for signout page:
signout.php:

<?php    
         session_start();
         session_destroy();
         header('location:login.php');
?>

           You select the remember me check box while login. Then you navigate to welcome.php page. Now you click the signout on welcome.php page, you can't return back to login page. Because the concept is, when you signout the page, you return back to login.php page, where you wrote the php script as like as:
          
                  if(isset($_COOKIE['name']) && isset($_COOKIE['pwd'])) {}

It means, if values in cookie, you can login again. Now you comes to login page and again login and navigates to welcome page because your values in cookies.So you are in welcome page until the cookie is expired.

Related Post:
Create Registration form using PHP and MySQL
Simple login form validation using php and mysql
Forgot password to mail in login form validation using PHP and Mysql

25 Jun 2014

How to store array values into cookies in PHP

                      Cookies can store only string values. It can't store array values. But we are try to add array values into cookies in PHP.

Store array into cookies in php using serialize():

                           
                    Consider the following PHP script:


<?php
error_reporting('E_ALL ^ E_NOTICE');
$val=array('name'=>'Guru','country'=>'USA');
$val1=serialize($val);
setcookie('values',$val1);
?>

             Where,
                serialize() - Generate a storable values. Suppose you use above PHP code without serialize array value. Then you'll get error like this:

Store array values into cookies in php

After serialize the array values, you can store array values into cookies using setcookie() in PHP.
Now the cookie contains array values.

Retrieve array values from cookies in PHP using unserialize():

                       
                     Consider the following example:

<?php
error_reporting('E_ALL ^ E_NOTICE');
$val=array('name'=>'Guru','country'=>'USA');
$val1=serialize($val);
setcookie('values',$val1);
$dat=$_COOKIE['values'];
$data=unserialize($dat);
foreach($data as $key => $vl)
{
   echo $key.' : '.$vl.'<br>';
}
?>

           Where,
                     unserialize() - takes serialized values and converts it into PHP value again. If you didn't use unserialize() in above PHP code, then you'll get error like this:

Retrieve array values from cookies in php

$_COOKIES[''] is used to retrieve values from cookies.
Now you can get array values from cookies. The output is:

                 name : Guru
                 country : USA


Store multiple values into cookies in PHP using json_encode():


                         You can also store multiple values into cookies using json_encode() method in PHP.
json_encode()  returns JSON representation of a value.  Consider the example:

<?php
$val=array('name'=>'Guru','country'=>'USA');
$ar=json_encode($val);
setcookie('cook',$ar);
?>

                      After encoded the array values, you can store it into cookies. If you use above code without json_encode(), then you'll get warning error message.

Retrieve array values from  cookies in PHP using json_decode();


                      You can retrieve array values from cookies using json_decode() like unserialize() in PHP.
json_decode()  takes JSON encoded values and convert it into PHP value again. Consider the example:

<?php
$val=array('name'=>'Guru','country'=>'USA');
$ar=json_encode($val);
setcookie('cook',$ar);
$return=$_COOKIE['cook'];
$arr=json_decode($return, true);
foreach($arr as $key1 => $values)
{
  echo $key1.' : '.$values.'<br>';
}
?>

Now you can get output like this:
        
                 name : Guru
                 country : USA

Related Post:

2 Jun 2014

PHP - Cookies

                       Cookie is a file. It is used to identify the user. All of you see the login form with remember me check box. When you click it, it store your name in cookie. You can login again, when you open the page in your browser, even if you signout and close the browser. The cookie keep the information until it expired.

Cookies in php:


                       In php, you can
                                    1. Create cookies,
                                    2. Retrieve cookies,
                                    3. Delete cookies.

Create cookies in php:


                      setcookie() is used to create cookies in php. The syntax for create cookies as follows as:

                         setcookie(name, value, expire, path, domain)

     where,
                 - name is name of the cookie.
                 - value is value kept by cookie.
                 - expire is when the cookie expired. It is defined by time() + 60*60*24*30 (cookie set for 30 days). If set the expire is 0 or not defined, then the cookie is expired when close the session.
                 - path refers where the cookie is available. If the set '/', then the cookie will be available on whole domain. If you want to set cookies only particular folder, then you've to set path '/foldername/'. You can set for sub-folder too like '/folder/sub-folder/'. If it is not set, the cookies will be available only on current directory.
                 - domain refers where the cookies is available.

 Consider the following example:          

<?php    
          setcookie('name','guru', time()+ (60*5) );
?>

 where,
           - name is name of guru.
           - guru is the cookie values.
           - time()+ (60*5) means cookies expired after 5 minutes. If you want to set cookies in days, then use
time() + (60*60*24*1) where set expiration time for 1 day.
       

Retrieve cookies in php:


                         S_COOKIE['name'] is used to retrieve the cookie in php. Consider the following example:

<?php    
          setcookie('name','guru', time()+ (60*5) );
          echo $_COOKIE['name'];
?>

                   
             Now you'll get output:     guru

Delete cookies in php: 


                   You can delete the cookies by set expire time is past in php. Consider following example in php:
welcome.php:

<?php    
          setcookie('name','guru', time()+ (60*5) );
          echo $_COOKIE['name'];
          echo'<a href="signout.php">Signout</a>';
?>

 signout.php:

<?php    
           setcookie('name','guru',time() - (60*5));
  header('location:welcome.php');
?>

                When you run the welcome.php, your output like this:

                       guru
                       signout

                  When you click the signout, it delete cookies and navigates to welcome.php. Now you'll get output like below:

delete cookies in php
              signout

Related Post: