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:

<?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…



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.

<?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



}

isset, Cups. isset. (Undefined Index warnings coming your way with that if)

<?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…

Off Topic:

You know you love it :stuck_out_tongue_winking_eye:

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

// 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…

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?


<?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));
		}
		


	}
	
}
?>

Now you are analysing the $_GET and $_COOKIE values, so var_dump them to see what PHP is making these values out to be.


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
[fphp]isset[/fphp]
[fphp]empty[/fphp]

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.