I have read ALOT of information about session security, and have come up with this little piece of code.
Would appreciate if you guys took a look at it and told me if I need to change something to make it better and safer.
function cookie_auth(){
    if(isset($_COOKIE['cookie_name'])){
        $data = $_COOKIE['cookie_name'];
        list(,$username) = explode(':', $data);
        $sql = "SELECT * FROM tbl WHERE tbl.usrname= '$username'";
        $res = mysql_query($sql) or die(mysql_error());
        $row = mysql_fetch_array($res);
        $num_rows = mysql_num_rows($res);
        if ($num_rows==1){
            // AUTHENTICATE COOKIE VALUES
            $salt1 = sha1($row['alt_username']);
            $text = "constant_text_here";
            $salt2 = sha1($text);
            if($data == $salt1.':'.$username.':'.sha1($row['alt_username'].$salt2)){
                // USER IS AUTHENTICATED AND CORRECT
                $_SESSION['logged_in'] = true;
            }
        }
        else if ($num_rows!=1){
            // REDIRECT TO LOGIN PAGE
        }
    }//end if isset cookie
    // ELSE IF COOKIE ISN'T SET //
    else {
        // REDIRECT TO LOGIN PAGE
    }
}// end function cookie_auth //
// AUTHENTICATE USER //
if(!isset($_SESSION['logged_in']) || $_SESSION['logged_in']!==true){
        cookie_auth();
}
else if ($_SESSION['logged_in']===true){
    // FURTHER AUTHENTICATION //
    if($_SESSION['HTTP_USER_AGENT'] != sha1($_SERVER['HTTP_USER_AGENT']){
        header('Location: http://www.domain.com/login');
        session_destroy();
        die();
    }
}
What do you think so far?
Also something I have thought about: 
What code should I use if user isn't authenticated? Should I use "session_destroy" and then "die()"? Should I use "unset session"?
Thanks
 
     
    