1

我已经实现了这个安全登录脚本。一切都很好,但是我的文章编辑页面会中断会话或某事。所以如果我来到这个页面,它就可以工作,但是如果我离开这个页面,它就会让我退出。“断页”的代码如下:

<?php
  include "includes/db_connect.php";
  include "includes/functions.php";
  sec_session_start();
  $page = $_SERVER['REQUEST_URI'];
  if(login_check($mysqli) == true) {
    include "includes/admin-header.php";
    $dotaz = new mysqli(HOST, USER, PASSWORD, DATABASE);
    if(empty($_GET['id'])){
      $stmt_articles = $dotaz->prepare("SELECT id, title, text FROM articles ORDER BY id DESC LIMIT 1");
      $stmt_articles->execute();
      $stmt_articles->store_result();
      $stmt_articles->bind_result($id_article, $title, $text);
      $stmt_articles->fetch();
    } else {
      $id=$_GET['id'];
      if($stmt_articles = $dotaz->prepare("SELECT id, title, text FROM articles WHERE id = ? LIMIT 1")) { 
      $stmt_articles->bind_param('i', $id);
      $stmt_articles->execute();
      $stmt_articles->store_result();
      $stmt_articles->bind_result($id_article, $title, $text);
      $stmt_articles->fetch();
      }
    } 
?>
  <div id='editation'>
    <div class='edit-title'>
      EDITACE: <a href="#" class='active'>ČLÁNKY</a> / <a href="#">NASTAVENÍ</a> <a href="#">NÁHLED</a>
    </div>
    <div class='edit-submenu'>
      <?php
        $dotaz2 = new mysqli(HOST, USER, PASSWORD, DATABASE);
        if($stmt_vypis = $dotaz2->prepare("SELECT id, title, text FROM articles ORDER BY id DESC LIMIT 0,8")) { 
          //do prepare se imho pouzije limit ?,? a pak se to bude bindovat pokazde jinymi cisly pro scrollovani?
          //$stmt_articles->bind_param('i', $id);
          $stmt_vypis->execute();
          $stmt_vypis->store_result();
          $stmt_vypis->bind_result($id_a, $titulek, $s_text);
          while($stmt_vypis->fetch()){
            echo "<a class='item' href=\"clanky.php?id=".$id_a."\">
                    <img src=\"http://25.media.tumblr.com/avatar_6feb8634e3d0_128.png\"/>
                    <p class='item-title'>".substr($titulek, 0, 20)."</p>
                    <p class='item-author'>Admin</p>
                    <div>
                      <p class='item-teaser'>".substr(strip_tags($s_text), 0, 20)."</p>
                      <p class='item-time'>Před 2 dny</p>
                    </div>
                </a>";            
          }
        }
      ?>
    </div>
    <script type="text/javascript">
      $(document).ready(function(){
        $(".edit-submenu").niceScroll({cursorcolor:"rgba(0, 0, 0, 0.6)"});
      });
    </script>
    <form action="clanky.php" method="post">
      <input type="text" size="80" name="title" value="<?php echo "$title"; ?>" />
      <textarea name="text" cols="100" rows="30"><?php echo "$text"; ?></textarea>
    </form>
  </div>
<?php
    include "includes/admin-footer.php";
  } else {
    if(!headers_sent()){
      header('Location: ./index.php?error=1');
    }
  }
?>
4

1 回答 1

0

我必须编辑函数 sec_session_start() 并为 session_set_cookie_params() 明确声明我的域。

function sec_session_start() {
    $domain = 'example.com'; // note $domain
    $session_name = 'sec_session_id'; // Set a custom session name
    $secure = true; // Set to true if using https.
    $httponly = true; // This stops javascript being able to access the session id. 
    ini_set('session.use_only_cookies', 1); // Forces sessions to only use cookies. 
    $cookieParams = session_get_cookie_params(); // Gets current cookies params.
    session_set_cookie_params($cookieParams["lifetime"], $cookieParams["path"], $domain, $secure, $httponly); // note $domain
    session_name($session_name); // Sets the session name to the one set above.
    session_start(); // Start the php session
    session_regenerate_id(true); // regenerated the session, delete the old one.     
}
于 2013-04-23T20:55:53.807 回答