Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Set Cookies in PHP

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 716
    Comment on it

    Hi Reader's,
    Welcome to FindNerd, today we are going to discuss to set Cookies in PHP.

    A cookie is a client side server which identifies the users on the user's computer. A cookie is a small file that the server embeds on the user's computer.

    When a user sends a a request on the server by his computer on the same page then, the browser will send the cookie with the request.

    For creating Cookies we will use the setcookie() function.

    You can see syntax below

     setcookie(name, value, expire, path, domain, secure, httponly);

    In above syntax, there are multiple parameters, so let explain one by one

    1- Name:-This is a required parameter and it specifies the name of cookie
    2- value:-This  specifies the value of cookies and it is an optional parameter
    3- expire:-This is an optional parameter and specifies the time of cookie expire.
    4- path:- It  the server path of the cookie.(Optional)
    5- domain:- It  specifies the domain name of the cookie.(Optional)
    6- secure:- It  specifies  cookie should only be transmitted over a secure HTTPS connection.
     (optional)
    7- HTTP only:-This is optional and by default it is FALSE. If we set to TRUE the cookie will be accessible only through the HTTP protocol\

    If we want to modify in a cookie then,we should use the same function setcookie() with different argument values. If we want to delete the cookie after a fixed time then, we can set an expiration time in that function.

    you can see below example for better understanding

    <!DOCTYPE html>
      <?php
        $cookie_name = "user";//set the cookie same
        $cookie_value = "Manish";//set the cookie value
        setcookie($cookie_name, $cookie_value, time() + (36400 * 30), "/");//use setcookie()
      ?>
    <html>
      <body>
        <?php
          if(!isset($_COOKIE[$cookie_name])) {
             echo "Cookie named '" . $cookie_name . "' is not set!";
          }else{
            echo "Cookie '" . $cookie_name . "' is set!<br>";
            echo "Value is: " . $_COOKIE[$cookie_name];
          }
        ?>
      </body>
    </html>

    I hope this blog will help you to set a cookie in your web application.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: