PHP, Cookies and www…oh my!
It always amazes me how many little “gotchas” there are in web design. Here’s the sitch:
- Set a cookie using PHP at www.site.com/wordpress…set the cookie for the root: /
- Can read the cookie at www.site.com/wordpress
- Can NOT read the cookie at www.site.com/another_folder
- CAN read the cookie at site.com/another_folder (without the www)
After some digging found this article which shares how to set cookies so they can be accessed at www. or .
http://stackoverflow.com/questions/2345137/php-cookie-problem-www-or-without-www
Boo-ya!
Constants: Programming 101
I’m not the smartest knife in the cookie jar and sometimes I forget my most basic of basics. I was doing some PHP programming and here’s what I needed. I needed a “variable” that was global but I didn’t want to have to reference it inside every function as global before I used it. I racked my brain on what to do here. This “variable” was not to change as what it was, was the root path of a site (http://www.site.com) and I knew my site would be moving at least once…every time I moved I didn’t want to update 40 references to “http://www.site.com/”. What to do, what to do?
Finally I had an A-Ha moment…I started singing “Take On Me“. Then after that I realized my answer was simple and something I probably hadn’t used in a bajillion years. CONSTANTS!
For those that may have forgotten them a constant is sorta like a variable, but they never, ever, ever change once you set them. that’s why they are CONSTANTS (as in constantly the same…ok, that’s a bit murky, scratch that…)
So here is how you define a constant in PHP:
define("HOWDY", "Hello world. ");
Simple, eh?
Here is how you use it (well, one example):
echo HOWDY . "How you doin?";
See? Easy-peezy.
Now go, use constants!
PS. This is a PHP example, but you know constants are in pretty much every language. Right? Right!
Put wordpress on a site with an existing page.
So I had this site that already had a page up. I needed to install wordpress in the root and make some changes but I didn’t want wordpress to be live to the general public…after asking in 4 forums and not getting help I figure this out. I was thinking I should use .htaccess but I solved this with a stand alone page and a cookie checker added to the themes header.php
Here’s the good:
- Moved my current page from www.site.com to www.site.com/sub/
- Created a page accessible only to me on the site that sets and destroys a cookie in the root directory using something like this:
$action = $_GET['action']; if ($action == "create") { setcookie("dealvilletempcookie123", "yes", time()+(60*60*24*365),"/"); } if ($action == "destroy") { setcookie("dealvilletempcookie123", "no", time()+(60*60*24*365),"/"); } - In the header.php file of the WP theme I check the cookie.
If its set to YES then I do nothing (which opens the WP install in the root directory)
If its set to NO (or not set or set to anything else) then I use the PHP header function to redirect…do this at the very top of the header file, before you load an WP stuff. ITS THE LAW!
Whoop…there it is.
PHP Cookies and WordPress – Set That Path, Brother!
OK I don’t claim to understand this (as so many things I post here) but I figured since I worked this out I would post it!
I am working on a WordPress site and setting a cookie with PHP code. I was running this code:
setcookie("your-selected-location", $the_slug, time()+(60*60*24*365));
The problem is that I was getting odd results and when I checked Firebug in Firefox, looking at the cookies, this ONE line was setting TWO cookies with the same name but for 2 different paths!
Still not sure why, although I think its something about WP, but I fixed it by changing the code to this:
setcookie("your-selected-location", $the_slug, time()+(60*60*24*365),"/");
Which forces just 1 cookie to set for the / path. There it is!
Recent Posts
- IE, Forms, Image Submit Buttons = Bad Mojo
- A good .htaccess file
- It’s the little things
- Converting a site to WP but doing the right SEO stuff
- 301 Redirect and Canonicalization
Categories
- Android
- Cookies
- CSS
- Droid 2
- FireFox
- General Nerdiness
- God
- HTML
- IE8
- Internet Explorer
- Introduction
- Javascript
- jQuery
- Mexico
- Photoshop
- PHP
- Random Thoughts
- SEO
- Spyware
- Virus
- Web Design
- Win7
- Wordpress
