www.webdeveloper.com
Results 1 to 6 of 6

Thread: [RESOLVED] Setting Cookie to a City

  1. #1
    Join Date
    May 2009
    Posts
    248

    resolved [RESOLVED] Setting Cookie to a City

    I would like users to choose their city when they first come to the site and then this will be their preference from then on.

    Can someone help me with this coding. Thanks

  2. #2
    Join Date
    Jan 2009
    Posts
    3,346
    Use a form to get the user to input text or choose from a list or how ever you decide to get the data. Then look up cookie handling in php for how to create/read a cookie.

  3. #3
    Join Date
    May 2009
    Posts
    248

    Wrote some code but not redirecting on return visit.

    <?php
    if(isset($_COOKIE['FaskenDirectory'])) {
    $preference = $_COOKIE['FaskenDirectory'];

    header("location:main.php?city=$preference");
    exit;
    }

    //this adds 30 days to the current time
    $month = 2592000 + time();

    if (isset($_POST['Submit'])){
    $city=$_POST['city'];
    }
    setcookie('FaskenDirectory', $city, $month);

    header("location:main.php?city={$_POST[city]}");

    ?>

    <html>
    <head>
    </head>

    <body>
    <p class="head">Please select your home city.</p>
    <form name="FaskenDirectory" method="post" action="<? echo $PHP_SELF; ?>">
    <table cellpadding="10">
    <tr><td><input name="city" type="radio" VALUE="Vancouver">Vancouver</td>
    <td><input name="city" type="radio" VALUE="Calgary">Calgary</td>
    <td><input name="city" type="radio" VALUE="Toronto">Toronto</td>
    <td><input name="city" type="radio" VALUE="Ottawa">Ottawa</td>
    <td><input name="city" type="radio" VALUE="Montreal">Montreal</td>
    <td><input name="city" type="radio" VALUE="Quebec">Quebec</td>
    <td><input name="city" type="radio" VALUE="London">London</td>
    <td><input name="city" type="radio" VALUE="Paris">Paris</td>
    <td><input name="city" type="radio" VALUE="Johannesburg">Johannesburg</td></tr>
    <tr><td colspan="9"><input type="image" value="Submit" src="images/select.jpg" border="0"></td></tr>
    </table>
    </form>
    </body>
    </html>

  4. #4
    Join Date
    May 2009
    Posts
    248

    Tried a different approach but does NOT work.

    First I created an index page with the form and a check for the cookie.

    PHP Code:
    <?php
    if(isset($_COOKIE['FaskenDirectory'])) {
    $preference $_COOKIE['FaskenDirectory'];

    header("location:main.php?city=$preference");
    exit;
    }
    ?>
    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Directory</title>
    <link href="styles.css" rel="stylesheet" type="text/css">
    <link href="images/favicon.ico" rel="shortcut icon" type="image/x-icon" />
    <link href="images/favicon.ico" rel="icon" type="image/x-icon" />
    </head>
    
    <body>
    <div id="wrapper">
    <div id="content">
    <p class="head">Please select your home city.</p>
    <form name="FaskenDirectory" method="post" action="setcookie.php">
    <table cellpadding="10">
    <tr><td><input name="city" type="radio" VALUE="Vancouver">Vancouver</td> 
    <td><input name="city" type="radio" VALUE="Calgary">Calgary</td> 
    <td><input name="city" type="radio" VALUE="Toronto">Toronto</td> 
    <td><input name="city" type="radio" VALUE="Ottawa">Ottawa</td> 
    <td><input name="city" type="radio" VALUE="Montreal">Montreal</td> 
    <td><input name="city" type="radio" VALUE="Quebec">Quebec</td> 
    <td><input name="city" type="radio" VALUE="London">London</td> 
    <td><input name="city" type="radio" VALUE="Paris">Paris</td> 
    <td><input name="city" type="radio" VALUE="Johannesburg">Johannesburg</td></tr>
    <tr><td colspan="9"><input type="image" value="Submit" src="images/select.jpg" border="0"></td></tr>
    </table>
    </form>
    </div>
    </div>
    </body>
    </html>
    Then I created a second page to set the cookie

    PHP Code:
    <?php
    $month 
    2592000 time();

    if(
    $_POST['Submit']){
    $city=$_POST['city'];
    }
    setcookie("FaskenDirectory"$city$month);

    header("location:main.php?city={$_POST[city]}");

    ?>

  5. #5
    Join Date
    May 2009
    Posts
    248
    I believe I have the cookie set but I cannot get it to bypass the index page if the cookie is set.

    This is at the top of the index page
    PHP Code:
    <?php 
    if(isset($_COOKIE['FaskenDirectory'])) { 
    $preference $_COOKIE['FaskenDirectory']; 

    header("location:main.php?city=$preference"); 
    exit; 

    ?>

  6. #6
    Join Date
    Jan 2009
    Posts
    3,346
    In the first post you had a little leap of logic:
    if (isset($_POST['Submit'])){
    $city=$_POST['city'];
    }
    setcookie('FaskenDirectory', $city, $month);

    header("location:main.php?city={$_POST[city]}");
    Can you check in your browser if the cookie is actually being set? Also, if running as "localhost" cookies can be sometimes problematic.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles