开发者

Using .htaccess, mod_rewrite, and cookies to separate multiple logins?

I'm trying to figure out how to do something but I don't even know if I'm thinking in the right direction.

The main goal is to allow multiple logins, from different accounts in the same browser. User information is being stored in cookies, and I would like to implement multiple similar cookies that are associated with different virtual directories.

My current directory structure is simple:

/
/scripts
/includes
/images

I only need to affect the 开发者_开发知识库scripts directory.

I want a user to be able to access a virtual directory under the scripts directory, and have it see the contents of the scripts directory, but pass a cookie set to the virtual directory. So accessing /scripts/username/file.php actually accesses /scripts/file.php but passes the cookie set to /scripts/username/

I don't even know if this is possible. If it is possible, I'm gonna need help. :)


It was simple once I worked through the hard parts. :)

It's a very simple .htaccess once you get your head around the regex (that's something I still have to do)

/scripts/.htaccess

RewriteEngine on
RewriteBase /scripts
RewriteRule ^([^/]*)/(.*) $2

Here's a simple .php page to get the path and sets a cookie containing the directory name, on the current virtual directory.

/scripts/test.php

<?php
$dirs = preg_split('/\//', $_SERVER['REQUEST_URI']);
$dir = '/'.$dirs[1].'/'.$dirs[2];
setcookie('cookie_test',$dirs[2],time()+60*60,$dir);

echo '<html><head></head><body>';
echo $_SERVER['REQUEST_URI'].'<br>';
echo $dir.'<br>';
echo 'cookie = ',$_COOKIE['cookie_test'];
echo '</body></html>';
?>

First load it sees no cookie(of course) but second load and thereafter each arbitrary directory name remembers its own cookie.

As it is, it is just my proof of concept. And that's all I was looking for today.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜