SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: php and cookies

  1. #1
    SitePoint Addict
    Join Date
    Sep 2008
    Posts
    235
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Red face php and cookies

    i want to make the following using cookies
    When any user access the website, if this is the first time for him, he has to fill a form as follows



    Name

    Email

    City

    Country

    Telephone number [area code] + [phone number]



    After filling the above fields and submitting, the site wants all above to be saved on users machine as cookies, so that, after submission, when you close the browser, even the computer itself is closed and you opened the website again, you’ll not find the form with fields above, you’ll find instead the data inserted before.

    plz can anyone help me to do the following , i want to know if this need a database or not? and how to make it?
    thnx a lot

  2. #2
    SitePoint Enthusiast
    Join Date
    Jan 2006
    Location
    England
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It depends on what you are going to use the data for

    This is an example (untested)
    PHP Code:
    <?php
    if(isset($_COOKIE['Name'])) 
    {

        echo 
    "Hello "$_COOKIE['Name'] ."! <br />\n";
    }else{

        if(isset(
    $_POST['submit']))
        {
            
    setcookie("Name"$_POST['name']);
            
    setcookie("Email"$_POST['email']);
            
            
    header("location: /");
            die();

        }

    ?>
    <form action="" method="post">
        <input type="text" name="name" /><br />
        <input type="text" name="email" /><br />
        <input type="submit" name="submit" />
    </form>

    <?php
    }
    ?>

  3. #3
    SitePoint Addict
    Join Date
    Sep 2008
    Posts
    235
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thnx for your reply but i made this in page index.php


    <html>
    <body><form action="welcome.php" method="post">
    Name: <input type="text" name="name" id="name" />
    City: <input type="text" name="city" id="city" />
    <input type="submit" />
    </form></body>
    </html>


    and in welcome.php

    <html>
    <body>Welcome <?php echo $_POST["name"]; ?>.<br />
    You are From <?php echo $_POST["city"]; ?> .</body>

    <?

    $expire=time()+60*60*24*30;
    setcookie("user2", $_POST["name"], $expire);
    //setcookie("user", "", 60);

    ?>
    <?
    if (isset($_COOKIE["user2"]))
    echo "Welcome " . $_COOKIE["user2"] . "!<br />";
    else
    echo "Welcome guest!<br />";
    ?>
    </html>

    i tested if i got it from my form and after that i tried to print my cookir name but it did not work
    can u help me plz

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    setcookie() must be called before you output any html. Observe the sample you were given, and see the manual for more info.

  5. #5
    SitePoint Wizard lorenw's Avatar
    Join Date
    Feb 2005
    Location
    was rainy Oregon now sunny Florida
    Posts
    1,099
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    If I remember right the cookie will not be available on the page that you set the cookie on, it will be available on all pages after it gets set.
    What I lack in acuracy I make up for in misteaks

  6. #6
    SitePoint Addict
    Join Date
    Sep 2008
    Posts
    235
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i cant fix this problem till now ,,,, can anyone help me?
    i tried the solutions u told me above but it did not works

    plz i need help

  7. #7
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Post your latest code, inside tags, and descrive the problem you are having. Are you getting an error? Is it not displaying the name? Is it not asking for the name?


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
  •