登录www和www之间的问题

时间:2013-09-17 14:58:29

标签: php session web login

我在我的网站上有一个登录系统,如果我使用没有www的网址并登录它是正常的但当我通过添加www更改我的网站的网址时,网站将显示我的帐户作为注销。如果我通过删除www再次更改网址,它将显示我的帐户作为登录(不再登录) 我的登录功能是:

    $username = $_POST['username'];

    $password = md5($_POST['password']);

    $users = $GLOBALS['db']->query("SELECT * FROM users WHERE username='$username' AND password='$password'") or $GLOBALS['db']->raise_error(); // Leaving 'raise_error()' blank will create an error message with the SQL
    $users_number = $GLOBALS['db']->num_rows($users);
    if(!empty($users_number))
    {
        while($users_sql = $GLOBALS['db']->fetch_array($users))
        {
            $_SESSION['username'] = $username;
            $_SESSION['id'] = $users_sql['id'];
            $_SESSION['logged_in'] = 'true';
                header('Location:./');
        }
    }
    else
    {
        $error_msg = "Worng combination";
        header('Location:?page=login.php&login_error_msg='.$error_msg); 
    }

我想知道为了让www.example.com和example.com看到相同的登录凭据,我应该做些什么更改

2 个答案:

答案 0 :(得分:2)

http://www.example.comhttp://example.com是两个不同的域名。如果你想要一个合适的网站,请在.htaccess文件中使用301重定向。我的示例总是将用户重定向到www.example.com:

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
</IfModule>

答案 1 :(得分:1)

使用

void session_set_cookie_params ( int $lifetime [, string $path [, string $domain [, bool $secure = false [, bool $httponly = false ]]]] )

$ domain部分应为.your-domain.com默认为您输入的完整域名(www.your-domain.com或your-domain.com)...以{{1开头}你告诉PHP在所有子域周围工作会话cookie。这样,www.your-domain.com,your-domain.com和falafel.your-domain.dom将共享同一个会话。

有关详细信息,请参阅http://www.php.net/manual/en/function.session-set-cookie-params.php

相关问题