A cookie is a small file that the server embeds on the user’s computer. This data is sent and recieved with each browser that is used to view a page using cookies.
To create a cookie in PHP use the setcookie() function. Here is its syntax:
[code lang=”php”]
setcookie($name, $value, $expire, $path, $domain);
[/code]
So if you want to create a cookie for someone’s status that will last one day, the code would look something like this:
[code lang=”php”]
// name = status
// expire = current timestamp plus 24 hours
// path = current path “/”
setcookie(“status”,”user status here”,time()+(3600*24),”/”);
[/code]
So let’s say we want to create a cookie for every user that logs in and have it last for 12 hours. We need to run a login form and validate the login details to match someone on our database, and then create the cookies and set them to a specific time interval. Let’s make a function for this on function.php:
function.php
[code lang=”php”]
0){
$row = mysql_fetch_assoc($sql);
// User found, now let’s create the cookies for the user!
if(!$_COOKIE[“userid”] && !$_COOKIE[“username”]) {
setcookie(“userid”,$row[“userid”],time()+(3600*12),”/”);
setcookie(“username”,$username,time()+(3600*12),”/”);
}
return true;
} else {
return false;
}
}
?>
[/code]
Now if you have a simple form with two inputs, one for username and password, you can send these to a login page which will run the above function to validate and check the login. We’ll put this form on our index.php:
index.php
[code lang=”html”]
[/code]
Now on login.php we include that function and run it with the submitted username and password.
login.php
[code lang=”php”]
[/code]
Now we’ve made the user logged in. Basically we checked the mysql database for a user with those login details and upon finding a match, we created two cookies for that user. One being their userid (an ID we keep in our database for each user) and the other being their username.
So what if the user wants to log out? This is very easy when dealing with simply erasing the cookies we made. We place the following code on our index.php page.
[code lang=”php”]
[/code]
Now when the user visits index.php?do=logout it will run this logout code. If the cookies are found, it erases them and they will be treated as a guest based on how your script treats users without these cookies. This way we can check each user on every page they visit for these cookies to tell if they are a registered and logged in user or a guest.
[…] Read the rest here: PHP Cookies with Login – setcookie() […]
Good morning, thanks for the post? Is your blog a free theme or custom? I am intrigued by your blog. Is it possible to include this post on one of my blogs?, i will of course link to this page. Many Thanks
Wonderful article which has received me considering concerning the prospective of the idea. Actually actually remarkable.
Thanks because of this! I’ve been searching all more than the web for that data.
I am quite interesting in this topic hope you will elaborate more on it in future posts
Awesome tutes.. 🙂
Hello This is a great blog keep your good work and thank you for hvar in with me So nice to hear frome you.Thanks!
Thanks for this video…I have been brain storming for something just like this and thanks to you I have what I was looking for and not all those spam looking links that make you want to click them just to see what they lead too…my pet peave.
This is indeed a fantastic resource. Thank you for making this publicly available.
Thanks-a-mundo for the blog article.Really thank you! Will read on…
I feel the information pointed out in the subject is actually very useful. I have been participating in a research on the issue and your writings just covered a great deal of concerns I had. I’m writing an essay and term paper for my English course and recently following a bunch of blogs and forums to study.
When I just recently found your blog and have been following along, I was thinking I would write my first comment. I don’t know just what to say apart from I really enjoyed reading. Very good writings. I am going to continue coming to this website on a regular basis.
This is a best place for such kind of articles, your website is a inspiration for me. i got so very much benefits and great results after visiting here and the grace is increasing day by day in your posts. The above information is extremly essential.
I was quite pleased to find this web site.I wanted to thank you for this fantastic study I definitely enjoying each and every little bit of it and I have you bookmarked to check out new stuff you post.
Quite interesting piece of information. Many Thanks!
Super text, I will add this blog to my favorites.
I found your web log from AOL which is outstanding. Thank you for sharing such a good helpful article.
I’m impressed! Really informative blog post here my friend. I just wanted to comment & say keep up the quality work. I’ve bookmarked your blog just now and I’ll be back to read more in the future my friend! Also nice colors on the layout, it’s really easy on the eyes.
I fairly accept a person’s mindset, this blog is basically great, superior as well, refueling.
Hi there, I can’t are aware of how to include your internet site in my rss reader. Can you Allow me, please
Hi administrator I wallow in w/ ur content . may i copy this advice as my educate test ? thanks
Hello dude,i like this A New blog very much. attain u allow suggestion being my blog? thanks for A New attention
Hello dude,i like Your New site very a lot. attain u allow suggestion being my blog? thanks being Ones New attention
I like A New style friends
You are the man ! good bye
Great to view you back again. And again by having an interesting article.
@above me .lol Do you have another idea about my comment
Great story sir.i will bookmark your blog
Amazing friend, i hope you will see my aksibintang.com . Thanks
To integrate synamic functionalities in websites, PHP is one application that needs to be learned. It is good that there are sites that offer PHP programming consultancies or programming services. With these services, you can easily upgrade your site to a more dynamic one.
understood.. nice example
I changed the function.php file code to read a first and last name. Getting a syntax error with your function code: Having a devil of a time figuring it out – major newbee here. All code to this spot works fine with my DB opening and reading the $results…
// User found, now let's create the cookies for the user!
if(!$_COOKIE["FirstName"] &&!$_COOKIE["LastName"]) {.
setcookie("FirstName",$row["FirstName"], time()+(3600*12),"/");
setcookie("LastName",$row["LastName"], time()+(3600*12),"/");
}
return true;
} else { // < error here.
return false;
}
}
mysql_close();?> // < error here.
Do you have any ideas? No doubt there is a closing {} that I'm now familiar with or some combination.
So you aren’t using the Mysql part of this script correct?
I would troubleshoot the problem by checking each line of code. So check each cookie seperately like so:
if(isset($_COOKIE[‘FirstName’])){ echo $_COOKIE[‘FirstName’]; }
if(isset($_COOKIE[‘LastName’])){ echo $_COOKIE[‘LastName’]; }
This will check if they exist and then echo their values.
Are you using an editor that gives you an error log? What error are you getting exactly?
I found the problem, it was at my end drawing from the DB with the wrong parameters. Used an error trap to find it.
Now I have to figure out how to call on the cookie in each of the various pages in my ‘telephone book’ to compare the logged in user to the data base contact in the telephone book.
I’m very new at this, putting in a small DB for relatives to contact each other. But I don’t want one person modifying someone else’s information which they can currently do.