Not able to get value when i use cookie function

hello frnd plz help me out …i created this php form as index.php
<?php
if(isset($_POST[‘username’]))
{
$username1=$_POST[‘username’];
setcookie(‘clientname’,$username1);
// $password=$_POST[‘userpass’];
// setcookie(‘clientpass’,‘$userpass’);
}
// $u=$_POST[‘username’];
// echo $u;
?>
<html>
<head>
<title>login</title>
</head>
<body>
<div>
<form method=“post” action=“cookie.php”>
<label for=“name”>Name</label>
<input id=“name” type=“text” name=“username”>
<label for=“pass”>Pass</label>
<input id=“pass” type=“password” name=“userpass”>
<input type=“submit” name=“submit” value=“submit”>
</form>
</div>
</body>
</html>

and another page for displaying the username throw cookie page cookie.php
<?php

if(isset($_COOKIE[‘clientname’]))
{
echo $_COOKIE[‘clientname’];
}

?>

And the problem is that i am nt getting anything . A blank page pops-up not able to solve y its happening plz help me out where i am mistaking

Hi,

The cookie never gets set because your form submits directly to cookie.php, but the code which sets the cookie from the $_POST data is at the top of index.php and doesn’t run after the form is submitted.

it means that what ever value i enter in box it gets empty … and i need to set my cookie to cookie.php…did i get u rgiht my frnd

One way you could change your code to make it work would be like this:

index.php


<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>login</title>
</head>
<body>
    <div>
        <form method="post" action="cookie.php">
            <label for="name">Name</label>
            <input id="name" type="text" name="username">
            <label for="pass">Pass</label>
            <input id="pass" type="password" name="userpass">
            <input type="submit" name="submit" value="submit">
        </form>
    </div>
</body>
</html>

cookie.php


if(isset($_POST['username']))
{
    $username1 = $_POST['username'];
    setcookie('clientname', $username1);
}

Now $_COOKIE['clientname'] will be set and you can access it from other pages on your site.

Thanku sir thanku so much its working …:slight_smile: