如何在PHP中正确启动会话?

如何在PHP中正确启动会话?,php,session,login,session-variables,deprecated,Php,Session,Login,Session Variables,Deprecated,我最近在学习如何为我的网站定制CMS的教程。我目前正在制作后端,因此用户可以创建和编辑页面。本教程有点陈旧,因此各种函数都已被弃用。除了“session_register();”函数之外,我能够修复其中的大部分。我在很多网站上都看到过,包括这个告诉我使用“$\u SESSION['admin']=$username;”的网站。这对我不起作用,因为我将其应用于许多页面,每个页面都要求我重新输入信息 以下是我正在使用的文件: admin_check.php: <?php $error_msg=

我最近在学习如何为我的网站定制CMS的教程。我目前正在制作后端,因此用户可以创建和编辑页面。本教程有点陈旧,因此各种函数都已被弃用。除了“session_register();”函数之外,我能够修复其中的大部分。我在很多网站上都看到过,包括这个告诉我使用“$\u SESSION['admin']=$username;”的网站。这对我不起作用,因为我将其应用于许多页面,每个页面都要求我重新输入信息

以下是我正在使用的文件:

admin_check.php:

<?php
$error_msg="";
if ($_POST['username']){
$username=$_POST['username'];
$password=$_POST['password'];
// Simple hard coded values for the correct username and password
$admin="Admin";
$adminpass="Password";
//connect to mysql here if you store admin username and password in your database
//This would be the prefered method of storing the values instead of hard coding them here into the script
if(($username !=$admin)||($password != $adminpass)){
    $error_msg='<h3 class="text-danger">Login information incorrect, please try again.</h3>';
} else{
    $_SESSION['admin']=$username;
    require_once "index.php";
    exit();
}
}
?>

<?php
if ($_SESSION['admin']!="Admin"){
echo '<div class="container" style="background-color: #FFF ;"><h3><i   class="glyphicon glyphicon-alert text-danger"></i> Solo los administradores
      tienen permiso para ver este directorio. No se admite gente con Lag!        </h3><br/>

      <!DOCTYPE html
<html>
<head>
    <title>Login Administrador</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta charset="UTF-8">       
    <link href="../css/bootstrap.min.css" rel="stylesheet" />
    <link href="../css/styles.css" type="text/css" rel="stylesheet">
    <link href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-    glyphicons.css" rel="stylesheet">
     </head>
      <body style="padding-top: 5px; background-color: #EEE ;">
    <div style="max-width: 450px; max-height: 550px; background-color: #CCC; padding-top: 30px; box-shadow: 0 0 15px #DDD; border-radius: 10px" class="container">
        <div class="jumbotron" style="background-color: #fff; max-height: 470px; padding: 10px; border-radius: 2; box-shadow: 0 0 12px #777;">
            <legend class="text-success">LOGIN ADMINISTRATOR</legend>
            <hr />                
            <p style="font-size: 12pt;">Please enter your username and password to login into the admin site.</p>
            <form action="admin_check.php" method="post" target="_self" class="form-group">
                <label class="label label-default">username</label>
                <input type="text" class="form-control" placeholder="username" name="username" id="username">
                <br/>                    
                <label class="label label-default">password</label>
                <input type="password" class="form-control" placeholder="password" name="password" id="password">
                <br/>
                <button class="btn-success" value="submit" type="submit" name="button" id="button" >Submit</button>
            </form><br/>
            '.$error_msg.'
        </div>
    </div>
</body>
</html>

<br/><br/>

         <center> <a href="../" >Click aquí para regresar a un lugar seguro.</a>    </div></center>';


    exit();
    }
    ?>

^如您所见,管理员在这里输入用户名和密码,我希望以某种方式将其存储在cookie中,这样他们就可以自由导航,而无需反复输入用户名和密码

index.php:

<?php
session_start();
include_once "admin_check.php";?>
<?php
include "admin_header.php";
?>
<body id="home">
    <div class="container" style="background-color: white;">
        <h2 style="color: #FF691F"><i class="fa fa-home fa-2x"></i> Home</h2>
    </div>
</body>        
<?php include "admin_footer.php"; ?>
创建_blog.php:

 <?php
session_start();
include_once "admin_check.php";?>
<?php
include "admin_header.php";
?>
<script type="text/javascript">
function validate_form(){
    valid=true;
    if(document.form.pagetitle.value===""){
        alert("Please Enter a Page Title!");
        valid=false;
    }else if{
        alert("Please Enter Keywords about this Page!");
        valid=false;
    }
    return valid;
}
</script>
<script type="text/javascript">
function validate_form1(){
    valid=true;
    if(document.form.pid.value===""){
        alert("Please Enter a Page ID!");
        valid=false;
    }
    return valid;
}
</script>

<body id="blog">
<div class="container" style="background-color: white; box-shadow: 0 -5px 9px 3px #b2b2b2;">
    <br/>
    <h2 style="color: #777 ;"><i class="fa fa-book fa-lg"></i> Upload Blog Entry</h2>
    <p class="text-danger text-center">Title, Body, and Keywords are required to create new post! None should be left blank!</p>
    <a href="mmoAdmin/"><h6 class="text-muted pull-right"><< Back Home</h6>  </a>
    <br/>
    <form id="form1" name="form1" method="post" action="edit_blog.php" onsubmit="return validate_form1();" class="form-inline">
        <div class="form-group">
            <button class="btn-default" type="submit" name="button2" id="button2" value="Edit Post">Edit Post</button>
        </div>
        <input name="pid" type="text" id="pid" size="8" maxlength="11" class="form-control"/>
        <span id="helpBlock1" class="help-block">Enter the ID of the post you want to edit. eg: "blog/article.php?pid=<'this is the id'>"</span>
    </form>
    <br/>
    <form id="form2" name="form2" method="post" action="delete_blog.php" onsubmit="return validate_form2();" class="form-inline">
        <div class="form-group">
            <button class="btn-default" type="submit" id="button3" name="button3" value="Delete Post">Delete Post</button>
        </div>
        <div class="form-group"><input type="text" class="form-control" id="pid" name="pid"/></div>
        <span id="helpBlock2" class="help-block">Enter the ID of the post you want to delete. eg: "blog/article.php?pid=<'this is the id'>"</span>
    </form>
    <br/>
    <form id="form" name="form" method="post" action="congrat_blog.php" onsubmit="return validate_form();">
    <div class="form-group">
        <label>Title</label>
        <input type="text" name="pagetitle" id="pagetitle" class="form-control" placeholder="Title..." value="<?php echo $pagetitle; ?>"/>
        <br/>
        <br/>
        <label>Body</label>
        <textarea name="pagebody" id="pagebody" style="height: 480px; width: 100%" ><?php echo $pagebody; ?></textarea>
        <br/>
        <label>Keywords</label>
        <input type="text" name="keywords" id="keywords" class="form-control" placeholder="blog, mmob, etc..." value="<?php echo $keywords; ?>" />
        <br/>
        <button type="submit" class="btn-success" name="button" id="button"   value="Create this page now">Submit</button>
    </div>
    </form>
</div>

</body>


<?php include "admin_footer.php"; ?>

函数validate_form(){
有效=真;
if(document.form.pagetitle.value==“”){
警告(“请输入页面标题!”);
有效=错误;
}否则如果{
警告(“请输入有关此页面的关键字!”);
有效=错误;
}
返回有效;
}
函数validate_form1(){
有效=真;
if(document.form.pid.value==“”){
警报(“请输入页面ID!”);
有效=错误;
}
返回有效;
}

上传博客条目 创建新帖子需要标题、正文和关键词!没有一个应该留空


编辑文章 输入要编辑的帖子的ID。例如:“blog/article.php?pid=”
删除帖子 输入要删除的帖子的ID。例如:“blog/article.php?pid=”
标题
查看代码时,您忘记了PHP文件开头的
session\u start()。这通常会设置(或使用已通过的)Cookie,然后识别用户,这样会话变量就可以工作了


查看您的代码时,您忘记了在PHP文件开头的
session\u start()
,最好将其放在PHP文件的顶部,这样会话就可以工作了。这通常会设置(或使用已通过的)Cookie,然后识别用户,这样会话变量就可以工作了


问题:在代码中设置会话之前,您忘记启动会话

解决方案: 将第一部分代码替换为以下固定代码:

<?php
session_start();
$error_msg="";
if ($_POST['username']){
    $username=$_POST['username'];
    $password=$_POST['password'];
    // Simple hard coded values for the correct username and password
    $admin="Admin";
    $adminpass="Password";
    //connect to mysql here if you store admin username and password in your database
    //This would be the prefered method of storing the values instead of hard coding them here into the script
    if(($username !=$admin)||($password != $adminpass)){
        $error_msg='<h3 class="text-danger">Login information incorrect, please try again.</h3>';
    } else{
        $_SESSION['admin']=$username;
        require_once "index.php";
        exit();
    }
}
?>

注意:在尝试访问
$\u会话
数组或尝试设置一个数组之前,以及在将任何输出发送到浏览器之前,请记住在页面上运行
会话_start()
语句

参考URL:


问题:在代码中设置会话之前,您忘记启动会话

解决方案: 将第一部分代码替换为以下固定代码:

<?php
session_start();
$error_msg="";
if ($_POST['username']){
    $username=$_POST['username'];
    $password=$_POST['password'];
    // Simple hard coded values for the correct username and password
    $admin="Admin";
    $adminpass="Password";
    //connect to mysql here if you store admin username and password in your database
    //This would be the prefered method of storing the values instead of hard coding them here into the script
    if(($username !=$admin)||($password != $adminpass)){
        $error_msg='<h3 class="text-danger">Login information incorrect, please try again.</h3>';
    } else{
        $_SESSION['admin']=$username;
        require_once "index.php";
        exit();
    }
}
?>

注意:在尝试访问
$\u会话
数组或尝试设置一个数组之前,以及在将任何输出发送到浏览器之前,请记住在页面上运行
会话_start()
语句

参考URL:


因此,该教程没有提到必须在每个页面中启动会话,但确实如此

因此
session_start()但必须是使用会话的所有文件的一部分

错误报告将在以下方面对您有所帮助:

本教程可能没有涉及的另一件事是“密码”

使用以下选项之一:

  • PHP5.5的函数
  • 兼容性包(如果PHP<5.5)
其他链接:

关于列长度的重要旁注:

如果您决定使用
password\u hash()
或兼容包(如果PHP<5.5),请务必注意,如果当前密码列的长度小于60,则需要将其更改为60(或更高)。手册建议长度为255

您需要更改列的长度,并使用新的哈希重新开始,以使其生效。否则,MySQL将以静默方式失败

如果计划以后使用数据库,请与或一起使用

  • 这只是一个见解

因此,该教程没有提到必须在每一页中启动会话,但确实如此

因此
session_start()但必须是使用会话的所有文件的一部分

错误报告将在以下方面对您有所帮助:

本教程可能没有涉及的另一件事是“密码”

使用以下选项之一:

  • PHP5.5的函数
  • 兼容性包(如果PHP<5.5)
其他链接:

关于列长度的重要旁注:

如果您决定使用
password\u hash()
或兼容包(如果PHP<5.5),请务必注意,如果当前密码列的长度小于60,则需要将其更改为60(或更高)。手册建议长度为255

您需要更改列的长度,并使用新的哈希重新开始,以使其生效。否则,MySQL将以静默方式失败

如果计划以后使用数据库,请与或一起使用

  • 这只是一个见解

问题是,您是否启动了admin_check.php的会话?
session_start()将启动