Category Archives: Web Development

Cookie doesn’t set using PHP 4, but they do using PHP5?

So yesterday when working on a client site all of the cookies stopped working. I had not changed a single thing since I had last worked on it and everything worked. I did some research for a couple hours where people wrote about cookies not setting properly. It yielded a vast amount of results, mostly pointing to how I was setting the cookie. My cookie code appeared to be perfect in structure though so I cast that aside.

setcookie("cookieName", '1' , time()+7200,'/','.levijackson.net');

The best answer I could fine over the two hours was this:

Check the cookie settings of the other browsers and if they’re set to block all or empty on exit.
If the cookies work in one browser, but not another, you will need to make sure that the other browser is letting you set cookies in the first place.
Sometimes it will look like you can create the cookie, but then it will disappear or be deleted with each page reload.

It’s also possible that because you’re setting the cookies in an iframe, that the browsers may view it as a third-party cookie and reject it unless explicitly set out in the browser preferences to allow third-party cookies.

In that case you would need a compact privacy policy (or a compact P3P header) on the pages from where you’re trying to set the cookies from.
For PHP, you would add this as your header for the page setting the cookie:
header(‘P3P:CP=”IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT”‘);

Posted on Stackoverflow

The problem?

It didn’t work for me. For one, I wasn’t using iframes and therefore had no path issues. I also came across another solution of using httpOnly cookies. This solved the problem in Chrome but not IE. I depend on expiring and resetting cookie values for a key part of the site. This didn’t have the functionality of expiring from what I could find.

I always use it as my last option, as I like to figure things out on my own, but I finally turned to Stackoverflow. A user on the site, Kip, suggested I use Web Sniffer to see how the headers were being sent. When I viewed them I at first didn’t notice anything different. I was getting the following:

<em>(PHP 4 site)</em> eventCookie=2; expires=Wed, 30-Sep-2009 02:16:37 GMT; path=/; domain=.levijackson.net <br />
<em>(PHP 5 site)</em> eventCookie=2; expires=Wed, 30 Sep 2009 05:33:37 GMT; path=/; domain=.levijackson.net <br />

The part that finally clicked with me was when the cookies expired. For the PHP 5 site the cookie expired 5:30am GMT which was the equivalent to around midnight here (at the time of testing it was around 11pm). The cookie for the PHP 4 site however was set to expire around 2am GMT which translated to 9pm. As a result the cookie was setting and immediately unsetting.

The solution:

I set the cookie to expire in 24 hours rather than just 2. This allowed for the difference in server time.

Side notes:

I still don’t know how the server changed its time, but it quite literally went from working to not working. So I can only assume the admin of the server either updated or changed a setting. Either way I gained a tool and some more knowledge of cookies so I can’t complain.

When to use attributes and when to use elements in XML

So I was wondering today if there is a ruling on when to use attributes in XML. It seems to me it is much easier to parse out data if there are no attributes, but on the other hand I don’t want to put data that isn’t pertinent to the information being held. So my first option was this:

<location>
	<id>11</id>
	<name>Downtown</name>
	<address>225 Main Street Nowheresville</address>
</location>

My second option was this:

<location id="11">
	<name>Downtown</name>
	<address>225 Main Street Nowheresville</address>
</location>

I did some research and found that everyone and their mother was saying, data goes in elements, metadata in attributes. So I did some more research just to make sure I was understanding it completely. I found these two tips to be clearer.

  1. If the information is pertainant to the whole information as a whole, then it is best to make it an element.
  2. Conversely, if the information is used as a reference for other aspects of an application or web site, it is best to set it as an attribute.

Quite possibly the best tips I found were from the X12 Reference Model For XML Design.

  1. “Attributes are atomic and cannot be extended and its existence should serve to remove any and all possible ambiguity of the element it describes. They are ‘adjectives’ to the element ‘noun’.”
  2. “Use elements for data that will be produced or consumed by a business application, and attributes for metadata.”

Further reading: Principles of XML design: When to use elements versus attributes
Elements vs. attributes

MySQL notes

It has been some time since I took my intro to Relational Databases back in the spring of
07. So today I decided to do some research on MySQL and the database in general. So without further adieu, here are some helpful notes.

Data Types

  1. char vs varchar – If a string is inserted of 20 character in length is inserted into a char(30) field, it will take up 30 characters in the database. If that same string 20 characters in length is inserted into a varchar(30) field, it will take up just 20 characters of space.
  2. Int vs smallint – Smallint is used if your number will not exceed the range of -32768 – 32768. Int is used when the number falls between -2147483648 – 2147483648. It is also worthy to mention the other types of integers, tinyint, mediumint, and bigint

Normalization

Database normalization is quite possibly one of the blurriest areas for me. Going from teachers that profess a web site needs to be normalized to other teachers that say ‘if it works don’t fix it’ I have had to find a middle ground for now until I can make a decision and learn more about it. For most projects I do, I follow through to third normal form (3NF). This prevents from having redundancy, duplicate data, as well as prevents data anomalies from occurring. Data anomalies are incorrect/inconsistent data that is the result of updates or insertions that create “duplicate” records.

On another note, I just purchased a new MySQL book MySQL Crash Course by Ben Forta. It looks to be an excellent replacement for my current book. Just looking through the table of contents and reading a few pages, it covers way more than my previous book I purchased for my relational database class. So stay tuned for MySQL Notes ver.2.

Make all external links open in a new window/tab using jQuery

Alright, so I wanted to make a nice easy way to make all of the external links I post to open in a new tab. It just make sense to not facilitate users leaving your web site.

Step 1)
Target all of the anchor tags on the page when they are clicked.

Alright, so if that worked it means you did something right.

Step 2)
Get the url of the current page and split it down to the base URI for the page.

If that worked you will of course see your domain popup.

Step 3)
Check if the url is your own, if it isn’t add in the attribute of target=”_blank”

$(document).ready(function(){
	$('a').click(function(){
		var fullUrl = $(this).attr('href');
		var splitUrl = fullUrl.split("/");
		<strong>
		if((splitUrl[2] != 'www.levijackson.net') && (splitUrl[2] != 'levijackson.net')){
			$(this).attr("target","_blank");
		}
		</strong>
	});
});

If you aren’t familiar with the usage of $(this), it is shorthand for the element clicked on. To finish, obviously replace my domain with your own, and voilĂ .