Sunday, September 6, 2015

setcookie() function and $_COOKIE[] super global usage

<?php

if(isset($_GET['name'])){
   $name = $_GET['name'] ;
    setcookie('user_name', $name, time()+1000);
    $user_name = $name;
}

if(isset($_COOKIE['user_name'])){
$user_name = $_COOKIE['user_name'];
}
?>

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
</head>
<body>
<div class="container">
    <div class="row">
        <div class="col-md-4 col-md-offset-4">
            <br><br>
            <?php if(isset($user_name)){
                echo '<h1> Welcome ' . $user_name .'</h1>';
            }else{

             ?>
            <form method="get" action="">
               <div class="form-group text-center">
                   <label class="control-label" for="name">Name</label>
                   <input class="form-control" type="text" name="name" id="name">
               </div>
                <div class="form-group">
                    <input class="btn btn-block" type="submit" name="submit" id="name">
                </div>
            </form>
            <?php } ?>
        </div>
    </div>
</div>
</body>
</html>

No comments:

Post a Comment

css snippet for blogger code highlighting

code, .code {     display: block;     background: beige;     padding: 10px;     margin: 8px 15px; }