PHP help (cookies)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
NLE
Forum Newbie
Posts: 1
Joined: Thu Dec 17, 2009 4:10 am

PHP help (cookies)

Post by NLE »

Hello, i'm new to the forum. I'm an actionscript 3.0 developer, but need a php script for a current project. I put something together with php - and it works the way it should. I just want to know I coded it correctly or if it could be coded better since i'll be using it for a client. Basically trying to mimic sites that when you first goto and it asks you what location you want, then saves that in a cookie, and loads the site based on your selection.

This is what I came up with from online sources, and it works.

Code: Select all

 
<?php
ob_start();
$location =  $_POST['location']; 
$self =  $_SERVER['PHP_SELF']; 
 
if (isset($_COOKIE["savedLocation"]))
header($_COOKIE["savedLocation"]);
else
if(($location != null)) 
{ 
setcookie("savedLocation", $location, time()+(60*60*24*365));
header($location);
exit(); 
} 
?>
<!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>Cookie Text</title>
</head>
 
<body>
 
<form action ="<?php echo( $self ); ?>" method = "post"> 
  <label for="location">Location:</label>
  <select name="location" id="location">
    <option name="location" value="Location: http://www.nicholasleby.com/myFiles/Coo ... icago.html">Chicago</option>
    <option name="location" value="Location: http://www.nicholasleby.com/myFiles/Coo ... wyork.html">New York</option>
    <option name="location" value="Location: http://www.nicholasleby.com/myFiles/Coo ... geles.html">Los Angeles</option>
  </select>
  <label for="button"></label>
  <input type="submit" name="button" id="button" value="Submit" />
</form>
</body>
</html>
 
Thanks for any help or advice you can offer!!!

- Nick Eby
http://www.nicholasleby.com
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: PHP help (cookies)

Post by josh »

I guess if it works that's good, and since this isn't a big enterprise application I guess it would be fine for your project. As you grow and take on larger projects you will discover the concept of layering though. The first thing you should master is separating the 'logic' from the 'presentation'. One famous example is smarty (but a horrible framework to use). I use Zend Framework for templates personally, Zend_View can be used standalone
Post Reply