<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"><channel><title>LINICKX.com</title><link>https://www.linickx.com/</link><description></description><lastBuildDate>Fri, 08 May 2009 08:08:00 +0100</lastBuildDate><item><title>root-cookie - Tutorial 1: Accessing WordPress cookies from custom scripts.</title><link>https://www.linickx.com/root-cookie-tutorial-1-accessing-wordpress-cookies-from-custom-scripts</link><description>&lt;p&gt;I've been wanting to do this for a while, this is the 1st in a planned
short series of tutorials for &lt;a href="http://wordpress.org/extend/plugins/root-cookie/"&gt;my root cookie WordPress
plugin&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;I've decided to start with the purpose I wrote the plugin, then I'll
move onto a couple of tutorial which answer some of the FAQs I get.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Scenario.&lt;/strong&gt;&lt;br /&gt;
Before you start you need a working copy BLANK of WordPress, in a sub
directory, with a url like domain.com/wordpress.&lt;br /&gt;
A BLANK copy is a fresh install, using the default theme and only my
root-cookie plugin installed, remember after activating the plugin clear
your browsers cookies.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Getting Started.&lt;/strong&gt;&lt;br /&gt;
So you have a ready &amp;amp; waiting copy of WP, next create a directory
called "my-scripts" or whatever you like, and in it create 1.php with
the following contents:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;&amp;lt;?php print_r($_COOKIE); ?&amp;gt;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Browse to domain.com/my-scripts/1.php and you'll get a blank white page
with &lt;code&gt;Array()&lt;/code&gt;.&lt;br /&gt;
Next log into WordPress, and re-fresh 1.php and you should get
something like....&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Array ( [wordpress_xxxxxxxxxxxxxyyyyyyyyyyyyyy] =&amp;gt;
admin|1241455565|xxxxxxxxxxxxxyyyyyyyyyyyyyy
[wordpress_logged_in_xxxxxxxxxxxxxyyyyyyyyyyyyyy] =&amp;gt;
admin|1241455565|xxxxxxxxxxxxxyyyyyyyyyyyyyy )&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Done! You've just accessed WordPress's cookies :)&lt;/p&gt;
&lt;p&gt;Now when I first started I had a very basic script which hid my Google
adverts when I'm logged in, it's against Google's policies to click on
your own adverts so to avoid accidents I wanted to hide them.&lt;/p&gt;
&lt;p&gt;The following script is NOT secure, it doesn't check that you've logged
into WordPress it simply checks that you a cookie set with the right
username (&lt;em&gt;which anyone can fake&lt;/em&gt;) but for my purpose this was fine, I
had no issues with users faking cookies to get rid of the adverts*&lt;/p&gt;
&lt;p&gt;&lt;code&gt;&amp;lt;?php if (isset($_COOKIE["wordpress_logged_in_fxxxxxxxxxxxxxyyyyyyyyyyyyyy"])) {         $cookie = $_COOKIE["wordpress_logged_in_xxxxxxxxxxxxxyyyyyyyyyyyyyy"];         $cookie_elements = explode('|', $cookie);         if ($cookie_elements[0] == "admin") {                 echo "&amp;lt;h1&amp;gt;Hello admin!&amp;lt; &amp;lt;/h1&amp;gt;";         } } ?&amp;gt;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Replace admin with whatever username your using and job done! Next time
Accessing two WordPress installs domain.com/blog1 domain.com/blog2 with
root-cookie :)&lt;/p&gt;
&lt;p&gt;*&lt;em&gt;this will not work now, as I do something different.&lt;/em&gt; :)&lt;/p&gt;
&lt;p&gt;&lt;/code&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">nick</dc:creator><pubDate>Fri, 08 May 2009 08:08:00 +0100</pubDate><guid isPermaLink="false">tag:www.linickx.com,2009-05-08:root-cookie-tutorial-1-accessing-wordpress-cookies-from-custom-scripts</guid><category>howto</category><category>root-cookie</category><category>tutorial</category><category>WordPress</category></item></channel></rss>