SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Jun 2013
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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

  2. #2
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,433
    Mentioned
    45 Post(s)
    Tagged
    13 Thread(s)
    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.

  3. #3
    SitePoint Member
    Join Date
    Jun 2013
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  4. #4
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,433
    Mentioned
    45 Post(s)
    Tagged
    13 Thread(s)
    One way you could change your code to make it work would be like this:

    index.php
    HTML Code:
    <!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
    PHP Code:
    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.

  5. #5
    SitePoint Member
    Join Date
    Jun 2013
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanku sir thanku so much its working ..


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •