PHP, MySQL, JS, jQuery, Ajax, .htaccess,robots.txt,phponwebsites: June 2014

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:

23 Jun 2014

How to store array values into session in php

                You can store single value to session in PHP easily. Visit store values to session in php.


Store array values to session in PHP:


                          You can also store multiple values into session using PHP. You can store one or more than one values in array that can be stored into session. Consider the following example. It describes the array values directly stored into session in PHP. The following PHP script is used to store values to session in PHP.

<?php
 session_start();
 $ar=array('php','mysql','jquery');
 $_SESSION['arr']=$ar;
 $session=$_SESSION['arr'];
 foreach($session as $val)
 {
   echo $val.'<br>';
 }
 ?>

 where,
            session_start() - start the session
            $ar=array() - array variable

                  Now you can get output like this:

                                    php
                                    mysql
                                    jquery

Store multiple (array) values into session in PHP on form submit:

                          Consider the following example:


 <html>
 <body>
 <?php
 session_start();
 error_reporting('E_ALL ^ E_NOTICE');
 if($_POST['submit'])
 {
  if(count($_SESSION['arr'])==0)
  {
   $ar=array();
   $val=$_POST['value'];
   array_push($ar,$val);
   $_SESSION['arr']=$ar;
   print_R($_SESSION['arr']);
  }
  else
  {
   $val=$_POST['value'];
   array_push($_SESSION['arr'],$val);
   print_R($_SESSION['arr']);
  }
 }
?>
 <form action="#" method="post">
  <input type="text" name="value">
  <input type="submit" name="submit" value="Submit">
 </form>
 </body>
 </html>


                    The above example describes array values are storing into session in PHP while form submitting.
            where,
                      if(count($_SESSION['arr'])==0) {} - check  whether the array contains values or not. Create array variable and store value into array, then value is stored into session if there is no values in session. Otherwise it directly add values into session.
                     array_push() - add values to array in PHP.

Related Post:

16 Jun 2014

4 Types of errors in php

                       Errors will occur for following reason in php:
                              1.unclosed quotes,
                              2.missing semicolon,
                              3.missing parentheses,
                              4.add extra parentheses,
                              5.unclosed braces,
                              6.undefined variable,
                              7.call undefined function,
                              8.includes files which is not found

Common errors in php:


                    Basically four types errors is occurred in php. There are:

                              1. Parse error
                              2. Fatal error
                              3. Warning
                              4. Noticed error

Parse error:

                       
                             The parse errors php is the syntax error. It stops the execution of the script in php. The common reasons for occur parse in php as follows:
                              1.unclosed quotes
                              2.missing semicolon
                              3.unclosed braces
                              4.missing parentheses
                              5.add extra parentheses

Parse error by unclosed quotes in php:

                               
                           Consider the following example in php:

<?php
          $name='guru;
          echo $name;
?>

       where, the quotes are not closed. When you run this file, you'll get error message like this:

parse error by missing quotes in php

Parse error by missing semicolon in php:

                                     Consider the following example:

<?php
     $name='Guru'
     echo $name;
?>

          where, the semicolon is missing at $name. It gives the parse error like below:

Parse error by missing semicolon in php

Parse error by unclosed braces in php:


<?php
 $a=5;
 $b=6;
 if($a==$b)
 {
   echo 'equal';
 else
 {
   echo'Not equal';
 }
?>

     where the closing brace is missed. When you run this file, you'll get error message like below:

parse error by missing braces in php

Fatal error in php:


                The fatal error will occur when you call the undefined function in php. It stop the execution of the php script.

<?php
 function name($str)
 {
  echo $str;
 }
  echo place('guru');
?>

            where the function "place()" is not defined. But you call that function. Now it gives the fatal error like below:

fatal error in php

Waring in php:


                       The warning is occurred when you include the file but the file is not found in php. It does not stop the execution of the php script.

<?php
 include('welcome.php');
 echo'welcome'; 
?>

       where, the file welcome.php is not found but you include it. So you'll get output like this:

warning error in php

Noticed error in php:


                   The noticed error is occurred when you use undefined variable. It does not stop execution of the php script.

<?php
 $name='Guru';
 echo $guru;
?>

       where $guru is not defined but you accessed. So you'll get error message like this:

Noticed error in php

11 Jun 2014

Send mail using php

                       You can send emails using php.

Mail() in php:


                       The mail() in php is used to send mail. The syndex for mail() is:

                    mail(to, subject, message, header)

Where,
            to - receiver mail id.
            subject - subject of mail.
            message - message to be sent.
            header - header of mail.

Note:
           You can send mail if you are in server. You've to make some changes in php.ini file, if you send mail from local server.

          You've to make following changes in your php.ini file to send mail.
                     [mail function]
                     ; For Win32 only.
                     ; http://php.net/smtp
                       SMTP =your ISP's mail server address
                     ; http://php.net/smtp-port
                       smtp_port = 25


                     ; For Win32 only.
                     ; http://php.net/sendmail-from
                       sendmail_from =example@gmail.com

How to send mail using php:


                      The following php script is used for send mail:

<?php
   $to='example@gmail.com';         
   $subject='Send mail using php';
   $message='This mail send using php';
   $headers='From: guruparthiban19@gmail.com';
   $mail=mail($to,$subject,$message,$headers);
   if($mail)
   {
    echo'Mail send successfully';
   }
   else
   {
    echo'Mail is not send';
   }
 ?>

                      If the the mail is send, then you'll get successful message. Otherwise you'll get 'Mail is not send' message. Now you can see mail in inbox.

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: