SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2004
    Location
    london
    Posts
    145
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Setting / Retrieving Cookie based on Query String

    Hello. I am playing with a script that allows me to place weather data on my site. It works just fine and does what it is expected too.

    I am new with cookies and trying to get a cookie to set based on the query string (if available) so that when I come back to the page, the weather data loads.

    Here is the script I am running:

    Code:
    <?php
    
    $location =  $_GET['ICAO'];
    
    get_metar($location);
    
    	function get_metar($location) {
    	$fileName = "http://weather.noaa.gov/pub/data/observations/metar/stations/$location.TXT";
    	$metar = '';
    	$fileData = @file($fileName) or die('METAR not available');
    
    		if ($fileData != false) {
    			list($i, $date) = each($fileData);
    			$utc = strtotime(trim($date));
    			$time = date("D, F jS Y g:i A",$utc);
    
    				while (list($i, $line) = each($fileData)) {
    				$metar .= ' ' . trim($line);
    				}
    
    			$metar = trim(str_replace('  ', ' ', $metar));
    		}
    			echo "METAR FOR $location (Issued: $time UTC):<br>$metar";
    
    		}
    
    ?>
    You can see that $location = $_GET['ICAO']; is looking at the query string to see if there is a location using an airport identifier, ICAO (four letter assignment for all international airports).

    Basically, I want to add to the top of the script in plain language....

    Code:
    If echo $_COOKIE["weather"] <> ""
    
    $location =  $_COOKIE["weather"];
    
    If $_GET['ICAO']; = '"" {
    
    echo "Enter an airport......"
    
    else
    
    
    
    setcookie("weather", "$_GET['ICAO']; ", time()+3600);
    
    which continues to the rest of the script
    Yes, new to php but liking it. Any guidance would be appreciated.

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    PHP Code:
    <?php
    // start of every page that is going to access a cookie
    session_start();

    // a temp line of debug, so you can see what is going on
    var_dump($_SESSION);

    if(
    $_SESSION['weather']){
    // do stuff with the weather value


    }else{
    // take some other form of action



    }

  3. #3
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    isset, Cups. isset. (Undefined Index warnings coming your way with that if)

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    PHP Code:
    <?php
    // start of every page that is going to access a cookie
    session_start();

    // a temp line of debug, so you can see what is going on
    if( isset($_SESSION)){
    var_dump($_SESSION);
    }else{
    echo 
    "Session not set!";

    }

    if(isset(
    $_SESSION['weather'])){
    // do stuff with the weather value


    }else{
    // take some other form of action



    }
    Off Topic:

    nag nag nag...
    Last edited by AnthonySterling; Jun 22, 2011 at 07:33.

  5. #5
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    Off Topic:

    You know you love it ;P

  6. #6
    SitePoint Zealot
    Join Date
    Dec 2004
    Location
    london
    Posts
    145
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks but I am not sure if I understand. What you listed is for session right? I think I need to use a cookie here.

  7. #7
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by dutton View Post
    Thanks but I am not sure if I understand. What you listed is for session right? I think I need to use a cookie here.
    OMG, you are right, just substitute $_COOKIE for $_SESSION

    PHP Code:
    <?php

    // a temp line of debug, so you can see what is going on
    if( isset($_COOKIE)){
    var_dump($_COOKIE);
    }else{
    echo 
    "Cookie not set!";

    }

    if(isset(
    $_COOKIE['weather'])){
    // do stuff with the weather value

    }else{
    // take some other form of action
    // ie set the cookie
    }
    ?>
    Sorry, 'bout that, am usually more helpful than this you know...

  8. #8
    SitePoint Zealot
    Join Date
    Dec 2004
    Location
    london
    Posts
    145
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No problem at all. I'm still learning. I think I am getting closer and I believe my coding is correct however nothing is displaying on the page.

    Everything below get_metar($location) works just fine, it is my coding above that might be off a bit?

    Code:
    <?php
    
    // First let's see if there is a cookie value for weather location
    // NOTE: string variable must be in caps
    
    // a temp line of debug, so you can see what is going on
    
    
    if(isset($_COOKIE['weatherwx'])) {
    
    	$location =  $_COOKIE['weatherwx'];
    
    }else{
    	
    // Get ICAO from query string
    
    $location =  $_GET['ICAO'];
    
    
    // If no ICAO string value
    
    if ($location = '') {
    	
    		echo "Print Form to set Weather Station";
    	
    	} 
    	
    
    if ($location <> '') {
    
    
    get_metar($location);
    
    	function get_metar($location) {
    	$fileName = "http://weather.noaa.gov/pub/data/observations/metar/stations/$location.TXT";
    	$metar = '';
    	$fileData = @file($fileName) or die('METAR not available');
    
    		if ($fileData != false) {
    			list($i, $date) = each($fileData);
    			$utc = strtotime(trim($date));
    			$time = date("D, F jS Y g:i A",$utc);
    
    				while (list($i, $line) = each($fileData)) {
    				$metar .= ' ' . trim($line);
    				}
    
    			$metar = trim(str_replace('  ', ' ', $metar));
    		}
    			echo "METAR FOR $location (Issued: $time UTC):<br>$metar";
    			setcookie("weatherwx", $_GET['ICAO'], time()+(60*60*24*365));
    		}
    		
    
    
    	}
    	
    }
    ?>

  9. #9
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Now you are analysing the $_GET and $_COOKIE values, so var_dump them to see what PHP is making these values out to be.

    PHP Code:
    if ($location '') {//... 
    This line is clearly wrong, using a single = means you are assigning $location to be an empty string, when you want to be assessing if it is an empty string with == or preferably ===

    var_dump is important because it tells you what your condition should be testing for in terms of value and type.

    To save you floundering read the following carefully:

    PHP: Comparison Operators - Manual
    isset
    empty

    and then read and marvel at this grid, until the truth of it sinks in.
    PHP Variable and Array Tests

    Not understanding the message in the links above will lead to very many bad days for you in the future.


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
  •