Re: <% init
| From: | Teodor Cimpoesu | Date: | Mon, 23 Oct 2000 07:34:07 +0000 |
| Subject: | Re: <% init | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-21730@lists.php.net to get a copy of this message | ||
Hi Tim!
On Sun, 22 Oct 2000, Tim Sawyer wrote:
> Not that simple. I'm setting a cookie and it won't work in a function. Here's
> a code snip;
>
> 1 <?php
> 2 //Do the cookie thing.
> 3 if ($go) {
> 4 //write the cookies if Go button pressed, expires 90 days
> 5 setcookie("selvar[company]", $company, time() +7776000, "/");
> 6 setcookie("selvar[location]", $location, time() +7776000,
> "/");
> 7 } else {
> 8 //Initialize vars from cookie if not
> 9 $company=$selvar[company];
> 10 $location=$selvar[location];
> 11 }
> 12 ...
> 39 ?>
> 40 <html>
> 41 <head>
> 42 <title>...
>
I am convinced in my recommandation :-P, let's see:
<%init()%>
<html>
<%
//
// page stuff
//
%>
</html>
<%
function init()
{
global $HTTP_GET_VARS,$HTTP_COOKIE_VARS;
$GP = &$HTTP_GET_VARS;
$GK = &$HTTP_COOKIE_VARS; //shorthands
if ($GP['go']) {
//write the cookies if Go button pressed, expires 90 days
setcookie("selvar[company]", $GP['company'], time() +7776000,
"/");
setcookie("selvar[location]", $GP['location'], time() +7776000,
"/");
} else {
//Initialize vars from cookie if not
$GP['company']=$GK['selvar']['company'];
$GP['location']=$GK['selvar']['location'];
}
}
%>
I rewrote your function with the asumption of using
track_vars on',register
globals off', which is `Generally Recommended'[tm].
if you still like the lazy-programmer-way :^) of having all the vars at
hand (w/o knowing where are they coming from though), you can do an
extract($GLOBALS) right at the start of the function, to import all the vars
into your local scope.
-- teodor