PHP+Apache實(shí)現(xiàn)二級(jí)域名之間共享cookie的方法
本文實(shí)例講述了PHP+Apache實(shí)現(xiàn)二級(jí)域名之間共享cookie的方法。分享給大家供大家參考,具體如下:
簡(jiǎn)介
login.koastal.com設(shè)置domain為koastal.com,則www.koastal.com即可訪問(wèn)該cookie。
也就是說(shuō)二級(jí)域名(或者三級(jí)域名)之間可以通過(guò)這種方式共享cookie。
其實(shí)cookie的domain屬性的用法跟path有些類(lèi)似,比如我們將cookie的path設(shè)置為/,則/user目錄下也可以訪問(wèn)該cookie。
實(shí)踐
Apache同一端口對(duì)應(yīng)不同域名
httpd-vhosts.conf
<VirtualHost 127.0.0.1:80> ServerName koastal.com ServerAlias www.koastal.com DocumentRoot D:\wamp64\vhosts\www <Directory "D:\wamp64\vhosts\www"> Options +Indexes +Includes +FollowSymLinks +MultiViews AllowOverride All Require local </Directory> </VirtualHost> <VirtualHost 127.0.0.1:80> ServerName login.koastal.com DocumentRoot D:\wamp64\vhosts\login <Directory "D:\wamp64\vhosts\login"> Options +Indexes +Includes +FollowSymLinks +MultiViews AllowOverride All Require local </Directory> </VirtualHost>
修改本機(jī)hosts
windows
C:WINDOWS/system32/drivers/etc/hosts
linux
vim /etc/hosts
在最后添加
127.0.0.1 www.koastal.com login.koastal.com
編寫(xiě)php代碼進(jìn)行測(cè)試
D:\wamp64\vhosts\www\index.php
<?php if(isset($_COOKIE['username'])){ echo "welcome to ".$_COOKIE['username']; var_dump($_COOKIE); }else{ ?> <h1><a >Login</a></h1> <?php }
D:\wamp64\vhosts\login\index.php
<form action="#" method="post"> <p>username:<input type="text" name="username"></p> <p>password:<input type="password" name="password"></p> <p><input type="submit" name="submit" value="submit"></p> </form> <?php if(isset($_POST['submit'])){ setcookie('username',$_POST['username'],0,'/','koastal.com'); setcookie('password',$_POST['password'],0,'/','koastal.com'); header('Location:http://www.koastal.com'); }
親測(cè)可用,全文完~
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《PHP中cookie用法總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語(yǔ)法入門(mén)教程》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》及《php字符串(string)用法總結(jié)》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
Fatal error: Allowed memory size of 134217728 bytes exhauste
這篇文章主要介紹了Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 2611816 bytes)錯(cuò)誤的解決方法,需要的朋友可以參考下2014-11-11解決PHP 7編譯安裝錯(cuò)誤:cannot stat ‘phar.phar’: No such file or direc
這篇文章主要給大家介紹了關(guān)于解決在PHP 7編譯安裝遇到的錯(cuò)誤錯(cuò)誤:cannot stat ‘phar.phar’: No such file or directory問(wèn)題的相關(guān)資料,文中給出詳細(xì)的解決方法,需要的朋友可以參考借鑒。2017-02-02配置eAccelerator和XCache擴(kuò)展來(lái)加速PHP程序的執(zhí)行
這篇文章主要介紹了配置eAccelerator和XCache擴(kuò)展來(lái)加速PHP程序的執(zhí)行的方法,XCache和PHP5.5以來(lái)自帶的Zend Opcache一樣都是在共享內(nèi)存中存儲(chǔ)Opcode的緩存器,需要的朋友可以參考下2015-12-12PHP Zip壓縮 在線對(duì)文件進(jìn)行壓縮的函數(shù)
PHP在線對(duì)文件進(jìn)行Zip 壓縮函數(shù)代碼,用于使用PHP在線創(chuàng)建ZIP壓縮文件。2010-05-05完美解決dedecms中的[html][/html]和[code][/code]問(wèn)題
完美解決dedecms中的[html][/html]和[code][/code]問(wèn)題...2007-03-03PHP實(shí)現(xiàn)的只保留字符串首尾字符功能示例【隱藏部分字符串】
這篇文章主要介紹了PHP實(shí)現(xiàn)的只保留字符串首尾字符功能,結(jié)合實(shí)例形式分析了php隱藏部分字符串相關(guān)的字符串遍歷、截取相關(guān)操作技巧,需要的朋友可以參考下2019-03-03