Php 使用cookie和会话登录

Php 使用cookie和会话登录,php,session,cookies,session-cookies,Php,Session,Cookies,Session Cookies,我怎样才能保证饼干的安全 我想通过会话和cookies登录,我尝试这样做 这是我的密码 <?php $my_username =IsSet($_POST['username']) ? make_it_safe($_POST['username']) :Null; $my_userpass =IsSet($_POST['userpass']) ? make_it_safe($_POST['userpass']) :Null; $saveinfo =IsSet($_P

我怎样才能保证饼干的安全

我想通过会话和cookies登录,我尝试这样做

这是我的密码

<?php
$my_username  =IsSet($_POST['username'])   ? make_it_safe($_POST['username'])   :Null;
$my_userpass  =IsSet($_POST['userpass'])   ? make_it_safe($_POST['userpass'])   :Null;
$saveinfo    =IsSet($_POST['save_info'])  ? make_it_safe($_POST['save_info'])  :Null;

if(empty($my_username)){
  echo "<div class='alert alert-error'>Insert Username</div>";
}else if(empty($my_userpass)){
  echo "<div class='alert alert-error'>Insert Userpass</div>";
}else {
$my_userpass = sha1($my_userpass) ;

$select = $mysqli->query("SELECT * FROM members WHERE username='$my_username' AND userpass='$my_userpass' LIMIT 1");
$num = $select->num_rows;
if($num){
$rows = $select->fetch_array(MYSQL_ASSOC);

$id       = $rows ['id'];
$username = $rows ['username'];
$userpass = $rows ['userpass'];
if($username == $my_username && $userpass == $my_userpass){

$_SESSION['id'] = $id ;
$_SESSION['username'] = $username ;

if ($saveinfo == 'on'){
setcookie("id",$_SESSION['id'],time()+2592000);
setcookie("username",$_SESSION['username'],time()+2592000);
}
header("Location: home.php");
}

}else{
echo "Error";
}
?>


这是使用cookie登录的正确方法吗?安全吗?

为什么,当您有一个会话时,您需要一个具有其值的cookie?为什么不直接使用会话呢。另外,如果没有
session\u start()
,会话是如何工作的?然而,关于你的问题:
这既不正确,也不安全
不,你甚至还没有开始会话。在输出任何标题之前,使用页面顶部的
session_start()
启动会话。还有一点需要注意的是,尽管PHP对方法/函数名不区分大小写,但您不应该使用
IsSet()
,而它本机是
IsSet()
我使用
session_start()
ob start())
检查mysqli PDO驱动程序以防止sql注入