PHP基本会话用法

This is basic php and I'm a newbie.

Here's my code on the first file:

session_start();
$_SESSION['username'] = $username;
$_SESSION['password'] = $password;

header('Location: cms.php');

Here's the other one on another file:

if (session_id() == '') {
    header('Location: login.php');
}

Basically, I created a session on the first code. What I'm trying to do in the next is determine if a session was created, if not, it will just be redirected to the login page. The issue here is that there's no session created at all, well that's what I thought since I tried displaying session_id() and it did not show anything.

So now what am I missing? I'm guessing it has something to do with the header() function. But nonetheless, I need help and would really appreciate some explanation and better code for this.

Thanks a lot!

a better code:

session_start(); 
if (empty($_SESSION['username'])) {
    header('Location: login.php'); 
    exit;
}