Home >> Snippet List >> Snippet

Name
Cookie Login Code
Description
Shows a basic cookie-based "Remember Me" function
PHP Snippet
<?PHP
 
 IF( (ISSET($_COOKIE['USERNAME_COOKIE'])) && (ISSET($_COOKIE['PASSWORD_COOKIE'])) ) {
        $qry=MYSQL_QUERY("select * from loginTblName where username='".$_COOKIE['USERNAME_COOKIE']."' and password='".$_COOKIE['PASSWORD_COOKIE']."'") or DIE(MYSQL_ERROR());
        IF(MYSQL_NUM_ROWS($qry)!=0){
                $res=MYSQL_FETCH_ARRAY($qry);
                $_SESSION["ad_id"]        =    $res["ad_id"];
                $_SESSION["name"]        =    $res["fname"]." ".$res["sname"];
                $prevkeys                =   $res["priv"];
                $_SESSION["tempSessId"]    =    SESSION_ID();
                $keys                    = "";
                $prevkeys                = EXPLODE("#",$prevkeys);
                HEADER("location:filename.php");
            }
            ELSE{
                $err="Invalid Username or Password.";
            }
    }
 
IF(ISSET($_POST["submitLogin"])){
        $qry=MYSQL_QUERY("select * from loginTblName where username='".ADDSLASHES($_POST["UserName"])."' and password='".BASE64_ENCODE($_POST["password"])."'") or DIE(MYSQL_ERROR());
        IF($_POST["signedin"]=='1'){
            $_POST['password']=BASE64_ENCODE($_POST['password']);
            $hour = TIME() + 60*60*24*30; 
            SETCOOKIE(USERNAME_COOKIE, $_POST['UserName'], $hour);
            SETCOOKIE(PASSWORD_COOKIE, $_POST['password'], $hour);    
            //print_r($_COOKIE);
        }
        IF(MYSQL_NUM_ROWS($qry)!=0){
            $res=MYSQL_FETCH_ARRAY($qry);
            $_SESSION["ad_id"]        =    $res["ad_id"];
            $_SESSION["name"]        =    $res["fname"]." ".$res["sname"];
            $prevkeys                =   $res["priv"];
            $_SESSION["tempSessId"]    =    SESSION_ID();
            HEADER("location:filename.php");
        }
        ELSE{
            $err="Invalid Username or Password.";
        }
    }
 
?>