How to Check if Cookies are Enabled Using PHP 


               


How to Check if Cookies are Enabled Using PHP



PHP


In this article, we show how to check if cookies are enabled or disabled using PHP.

Cookies are data that is stored by a user's web browser. This can be data such as the information that a user enters into a form such as the user's name, email, telephone number, etc.

PHP has the functionality to work with cookies.

For example, you can write code that could send a cookie that contains the user's name. The cookie could then be stored on the user's computer and the next time he visits the site, teh cookie would be sent to your program, which could then result in a personalized greeting.

The first step, however, is to find out whether cookies are enabled and you can do this using PHP.

How do we do this?

The simplest way is probably to just create a cookie with PHP and check to see whether the cookie gets created. If it does and we're able to read it, then cookies are enabled with the broswer. If not, then cookies are disabled.

The PHP code below checks to see whether or not cookies are enabled.



So we use the PHP setcookie() function to create a cookie called testcookie and we give it the value of Peter. This is simply for testing.

We then check to see if the testcookie cookie has been set. If it has, cookies are enabled. If it hasn't, cookies are disabled.

Even PHP has good functionality for cookies, cookies are can present with bad data. Cookies need to be sanitized first before you can turst it. For instance, once your program sends a cookie to a visitor's browser, the visitor can edit or change that cookie to be anything he wants. This is why cookies aren't that secure. Sessions are more secure in that they are stored over the server and aren't accessible like cookies.

However, cookies still have their uses and if you aren't worried about security can work similarly to sessions.

So this is just real quick code to check whether or not cookies are enabled.


Related Resources

How to Create a New Directory Using PHP

How to Upload Files to Your Server Using Your Own Website

How to Restrict the Size of a File Upload with PHP

How to Create Your Own File Transfer Protocol (FTP) For Your Website Using PHP

How to Create a Register and Login Page Using PHP

How to List All Files in a Directory using PHP

How to List All the Directories of a Website Using PHP

How to List All Files of the Home Directory and Its Subdirectories of a Website Using PHP

How to Read the Contents of a File Using PHP

How to Write Contents to a File Using PHP

How to Copy a File Using PHP

How to Rename a File Using PHP

How to Delete a File Using PHP



HTML Comment Box is loading comments...