I have a PHP Yahoo weather script consisting of a form where you input your zip code. This gets a Yahoo weather xml document which displays on a second page.

This works fine, except the user must input his zip code each time he visits the site. I'd like to customize it so users can enter their zip code once and have their local weather appear whenever they visit the page.

i need someone can help me how to write that session cookies for zip code, I have tried that myself by put

<?php 
  $zip = $_POST['zip'];
  if( ( $zip != null ) ) 
    { 
     setcookie( "zip", $zip,  time()+3600*24*30*365); 
}
?>

for some reason is not working.

I have attahced full file for you to download and test it with session cookies. please help me thanks.

AM

    You should set method="post" to use $POST

    <?php if(!isset($_POST['zip'])) 
            { // POST form returns 'zip' and 'u' ?>
    
    <form method="post" action=""><div>
    	Enter a Zip Code: <input name="zip" size="5"/><br/>
    	Units: <label><input type="radio" name="u" value="f" checked="checked"/> Farenheit</label>
    	<label><input type="radio" name="u" value="c"/> Celsius</label><br/>
    	<input type="submit" value="Submit"/>
    </div></form>
    <?php
          }else {
    	include('weather.class.php');
    	$ret = weather($_POST['zip'], ($_POST['u'] == 'c'));
    
    echo "<table cellpadding=\"2\" cellspacing=\"4\">\n";

    Also about setcookie.

    <?php 
      $zip = isset($_POST['zip']) ? $_POST['zip'] : "";// if not set use empty string
      if(!empty($zip)) 
        { 
         setcookie("zip", $zip,  time()+3600*24*30*12); //for 1 year, 12 months
        }
    ?>

      Also note that a [man]session[/man] is not the same as setting a [man]cookie[/man]... the two topics have several differences.

        Alidad;10942392 wrote:

        I have a PHP Yahoo weather script consisting of a form where you input your zip code.

        Yahoo Weather is based on 5 digits ZIP Code
        ( http://developer.yahoo.com/yui/examples/connection/weather.html )

        This makes it very easy to make a <form maxlength="5">
        (maxlength="5" makes it impossible to enter longer inputs)
        and use [man]preg_match/man to make sure only 5 digits are entered in form

        My code below is a script that shows how you can test the input
        and maybe send the user back to the form, until ZIP Code is correctly entered.

        The preg_match pattern string means this:
        full pattern string = '#\d{5}$#'

        are only markers for delimiting

        so, the test pattern is:
        \d{5}$

        ^ = the beginning of string to be tested
        \d{5} = digits 5 (5 digits)
        $ = the end of string to be tested

        <?php
        
        $error_message = 'Welcome!';
        extract($_POST); //Extract $_POST Array into normal variables, like $zip
        
        if(isset($zip)){ //form actually entered?
        	if( preg_match( '#^\d{5}$#',  $zip) ){ //valid 5 digits zip?
        		$error_message = 'good';
        	}else{
        		$error_message = 'not valid zip code';
        	}
        
        if($error_message == 'good'){
        	//here we get the weather from Yahooo
        	echo 'Get Weather!';
        
        
        	exit(); //end script after show weather
        }
        }
        
        ?>
        <form method="post" action="">
        <input type="text" name="zip" maxlength="5">
        ZIP CODE 5 Digits (like: 12345)<br />
        <input type="submit" name="zipsubmit" value="GET Weather">
        </form>
        <?php
        	echo $error_message;
        ?>
          Write a Reply...