bool setcookie(string name[,string value[,int expirel[,string path[,string domain[,int secure]]]]].
第一个:name,必选参数,这个是cookie的变量名,可以通过$_COOKIE['user']调用变量名为user的cookie.
第二:value,可选参数,这个cookie变量的值,比如说setcookie("user","php"),我们通过调用$_COOKIE['user']可以得到php值;
第三个:expire,可选参数,这个是用来设置cookie变量保存的时间,注意是我们设置的的UNIX时间戳减去当前的UNIX时间戳才是cookie变量保存的时间。(UNIX时间戳:是从1970年1月1日(UTC/GMT的午夜)开始所经过的秒数) ,一般我们可以通过time()函数获取当前的UNIX时间戳,再加上我们要保存的时间(单位为秒)比如说,setcookie("user","php",time()+3600),这样我们就可以保存user这个cookie变量的时间为3600秒。另外我们可以通过设置的时间戳小于当前的时间戳来删除cookie变量,比如说setcookie("user","php",time()-1)这样我们就删除了user这个cookie变量了。
第四个:path,cookie的有效范围,这个参数是下一个参数domain基础上的有效范围,如果path设置为"/",那就是在整个domain都有效,比如setcookie("user","php",time()+3600,"/"),这样我们domain下的任何目录,任何文件都可以通过$_COOKIE['user']来调用这个cookie变量的值。如果path设置为"/test",那么只在domain下的/test目录及子目录才有效,比如domain下有两个目录: test1,test2,我们设置为setcookie("user","php,time()+3600,"/test1"),那么只有test1目录下才能通过$_COOKIE['user']调用user这个cookie变量的值,test2目录下获取不到。
第五个:domain,cookie有效的域名,如果domain,设置为googlephp.cn,那么在googlephp.cn下的所有子域都有效。假设googlephp.cn有两个子域,php.googlephp.cn,css.googlephp.cn,我们设置为setcookie("user","php",time()+3600,"/","php.googlephp.cn"),那么只有在php.googlephp.cn这个子域下才能获取user这个cookie变量的值. 再举一个例子:setcookie("user","php",time()+3600,"/test","php.googlephp.cn"),那么只有在php.googlephp.cn这个子域下的test目录下才能获取user这个cookie变量的值.
第六个:secure,值cookie是否仅通过安全的https,值为0或1,如果值为1,则cookie只能在https连接上有效,默认值为0,表示cookei在http和https连接上都有效。
使用cookie技术计算网站的月访问量:
<?php
header("Content-type:text/html;charset=utf-8"); //选择utf-8编码
if(empty($_COOKIE['counter'])) //如果cookie不存在
$counter=1; //设置$counter的初始值为1;
else //如果cookie存在
$counter=$_COOKIE['counter']+1 ; //每刷新一次页面,将$conunter变量的值累计加1.
//每刷新一次页面,cookie变量的值都重新赋予新的$counter的值,也就是不断累计加1.
setcookie('counter',$counter,time()+2678400);//time()+2678400就是说保存2678400秒,也就是一个月。
echo "你是第 $_COOKIE[counter] 位访客!!"; //通过$_COOKIE['counter']调用cookie变量的值。
?>如有问题,欢迎指正。
« 上一篇:解决Cannot modify header information 苦命的程序员,冏:下一篇 »
发表于 2012年02月5日 1:23 下午
I am impressed by the quality of information on this website. There are a lot of good resources here. I am sure I will visit this place again soon.
[回复]
发表于 2012年02月5日 1:25 下午
some really prize content on this site, saved to my bookmarks .
[回复]
发表于 2012年02月5日 1:43 下午
Wow that was strange. I just wrote an very long comment but after I clicked submit my comment didn't show up. Grrrr... well I'm not writing all that over again. Regardless, just wanted to say wonderful blog!
[回复]
发表于 2012年02月5日 2:05 下午
http://www.incompany.com/blog.php?user=earnestbrown1211&blogentry_id=175313
[回复]
发表于 2012年02月5日 2:13 下午
Can I recently say thats a relief to uncover somebody who really knows what theyre discussing on the internet. You definitely discover how to bring a challenge to light and earn it essential. The diet need to read this and recognize this side on the story. I cant think youre not a lot more popular as you surely hold the gift.
[回复]
发表于 2012年02月5日 2:57 下午
Ist Ihre Stromrechnung zu hoch? Vergleichen Sie unter den Stromanbietern und senken Ihre Stromkosten um bis zu 65 % . online Stromanbieter vergleichen,Stromversorger wechseln und Stromkosten sparen!
[回复]
发表于 2012年02月5日 3:22 下午
Person services occasionally have different features not to mention fun-based activities as all those with household not to mention basic holiday packages.
[回复]
发表于 2012年02月5日 3:51 下午
If you're still on the fence: grab your favorite earphones, head down to a Best Buy and ask to plug them into a Zune then an iPod and see which one sounds better to you, and which interface makes you smile more. Then you'll know which is right for you.
[回复]
发表于 2012年02月5日 4:24 下午
Thank you for your site post. Velupe and I are already saving for a new ebook on this issue and your short article has made all of us to save money. Your thinking really responded to all our queries. In fact, a lot more than what we had acknowledged prior to when we discovered your great blog. I no longer nurture doubts and a troubled mind because you have attended to each of our needs above. Thanks
[回复]
发表于 2012年02月5日 5:18 下午
Wow! Thank you! I constantly needed to write on my site something like that. Can I implement a fragment of your post to my website?
[回复]
发表于 2012年02月5日 6:27 下午
I do love the manner in which you have presented this specific situation and it does supply me some fodder for consideration. On the other hand, from what precisely I have experienced, I simply just hope when the actual reviews stack on that individuals stay on issue and in no way get started upon a tirade regarding the news of the day. Yet, thank you for this superb piece and even though I do not necessarily go along with the idea in totality, I value your viewpoint.
[回复]
发表于 2012年02月5日 7:21 下午
Lots of thanks for all of the hard function on this blog. My niece takes pleasure in engaging in internet research and it truly is simple to grasp why. We learn all relating towards the compelling method you make valuable suggestions by indicates of your weblog and therefore attract contribution from visitors on this region although my princess is discovering plenty of items. Have fun with the rest with the new year. You’re carrying out a very good job.
[回复]
发表于 2012年02月5日 7:40 下午
Hi, i think that i saw you visited my web site so i came to “return the favor”.I am trying to find things to enhance my web site!I suppose its ok to use a few of your ideas!!
[回复]
发表于 2012年02月5日 11:26 下午
Definitely believe that which you stated. Your favorite reason seemed to be on the web the simplest thing to be aware of. I say to you, I certainly get irked while people consider worries that they plainly don't know about. You managed to hit the nail upon the top and defined out the whole thing without having side-effects , people could take a signal. Will probably be back to get more. Thanks
[回复]
发表于 2012年02月5日 11:28 下午
Awesome Post: http://www.googlephp.cn/archives/39. You do a fine job. Thanks again. roksa
[回复]
发表于 2012年02月6日 12:09 上午
100 % good thing about with all the program. usually there are some what you should
[回复]
发表于 2012年02月6日 3:57 上午
some truly select articles on this website , saved to my bookmarks .
[回复]
发表于 2012年02月6日 4:36 上午
Mature services quite often experience different features along with fun-based activities because those people inside relatives in addition to broad vacation holidays.
[回复]
发表于 2012年02月6日 4:58 上午
I have learned several important things as a result of your post. I would also like to express that there is a situation that you will apply for a loan and do not need a cosigner such as a Fed Student Support Loan. But if you are getting credit through a classic creditor then you need to be willing to have a cosigner ready to help you. The lenders can base their own decision on a few factors but the greatest will be your credit worthiness. There are some loan companies that will as well look at your work history and come to a decision based on that but in most cases it will be based on on your rating.
[回复]
发表于 2012年02月6日 5:11 上午
Lovely sharp post. Never considered that it was that effortless. Praises to you!
[回复]
发表于 2012年02月6日 5:49 上午
I like your writing style truly loving this site .
[回复]