php 6

Thanks Tutorial!

session

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=, initial-scale=1.0">
  <title>Document</title>
</head>
<body>

  <!-- session 会话 --> 

  <?php

  // 使用例:记住用户是否已经登录

  session_start(); // 启动会话 

  $_SESSION['userId'] = 733; // 使用 _SESSION 超全局变量存储数据

  // 现在去运行 test7.php ,只要把 session_start() 放在文件开头就可以访问这些数据。
  
  unset($_SESSION['userId']); // 清除单个变量
  session_unset(); // 清除所有变量
  session_destroy(); // 销毁整个会话

  echo $_SESSION['userId'];

  ?>


</body>
</html>

session-cfg.inc.php

<?php

ini_set("session.use_only_cookies", 1);
ini_set("session.use_strict_mode", 1);

session_set_cookie_params([
  'lifetime' => 1800,
  "domain" => "localhost",
  'path' => '/hm2ns/',  
  'secure' => true,
  'httponly'=> true,
]);

// 非常重要,确保创建 session 前都使用

session_start();

if (!isset($_SESSION['last_regeneration'])){// 定时更新 session id

  session_regenerate_id(true);
  $_SESSION['last_regeneration'] = time();

} 
else  {

  $interval = 60 * 30;
  if (time() - $_SESSION['last_regeneration'] >= $interval){

    session_regenerate_id(true);
    $_SESSION['last_regeneration'] = time();

  }
}

/*
  每个 php 文件前都使用
  require_once 'config.php';
  来确保 session 安全
*/

password-hashing.inc.php

<?php

$sensitiveData = "your password";
$salt = bin2hex (random_bytes(16)); 
$pepper = "!noip@Nov29,2025:dream";

$hash = hash('sha256' , $salt . $sensitiveData . $pepper);

echo $hash;

// above is how to hash in a general way

// for password we have special functions

$pwd = "hm2ns";

$cost = [
  'cost' => 12, // 设置计算成本,默认是 10,成本越高,计算越慢,但更安全
];

$hashResult = password_hash($pwd, PASSWORD_BCRYPT, $cost); // 自动生成 salt 并使用 bcrypt 算法进行哈希

$loginPwd = "hm2ns1";

if (password_verify($loginPwd, $hashResult)){ // 自动比对
  echo "its all right";
}
else{
  echo "something wrong";
}

// 将这个文件直接 require 到注册和登录的文件中即可
 

php OOP

<?php

class dingfei{

  // public 可以随意访问
  // protected 自己和子类能访问
  // private 只有自己能访问

  // 变量这一类 :prperties / fields 

  private $father;
  private $son;
  private $wife;

  // constructor 

  // __construct 关键字作为构造函数,当创建时即执行 
  public function __construct($father, $son = "DXJ") {// 这个 $father 和上面的 $father 不一样,只是占位符
    // 使用 placeholder 设置默认值如果调用时未进行传参

    $this -> father = $father;
  }
  // 其他的和 cpp 差不多吧

  // method

  public function getter() {
    // 使用 getter 决定是否分享函数值
    // 不要直接 -> 变量
  }

  public function setter() {
    // 使用 setter 可决定是否修改等一系列判断
    // 不要直接 -> 变量
  }

  protected function database_handler() {
    // 敏感函数用 protected,有需要时先造个子类继承过去再进行操作
  }
};

class dingfei1 extends dingfei{ // 作为 dingfei 的子类 可以方位 dingfei 中的 protected

}

/*
MVC 架构:  

module view controller

module:仅有这些函数能与数据库交互
view:显示到浏览器
controller:实现主要功能逻辑  

安全可扩展
*/
posted @ 2026-02-15 19:28  hm2ns  阅读(1)  评论(0)    收藏  举报