Saturday, July 5, 2014

How to set a cookie in PHP

A common way to capture useful information about your visitors is to track with cookies. In PHP, it's extremely easy.

Let's say, you want to capture information from a login and want to authenticate. You could use similar code to this.

$name = $_POST["form_name"];
$password = $_POST["form_pass"];

Next, compare it to other variable values to see if they match. Ideally, you would connect to mysql server and compare, but for brevity sake, we'll just use variable values.

$db_name = "db-name";
$db_pass = "db-pass";

if (($name == $db_name) && ($password == $db_pass)) {

setcookie("your_auth","Successful");

}

That's it! Next time, we'll go over $_COOKIE variable and how that let's you capture the values.

Check out my car info app if you get a chance

No comments:

Post a Comment