Q:

Login issue between www and without www

I have a login system in my website if i use the url without www and login it is normal but when i change the url of my website by adding www to it the website will show my account as logout.  and if i change the url again by removing www it will show my account as login (without login again)
my login function is :
    $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); 
    }

I want to know is there any change that i should make to make the www.example.com and example.com see the same login credentials 

A:

http://www.example.com and http://example.com are two different domains. if you want a proper website, please use a 301 redirect in your .htaccess file. My example always redirects user to www.example.com:
<IfModule mod_rewrite.c>
    RewriteEngine On

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

