declare properties, use session without cookies

This commit is contained in:
Adrien MALINGREY 2025-05-06 18:20:08 +02:00
parent 4b92464c94
commit 310a1883d2
3 changed files with 32 additions and 18 deletions

View File

@ -18,7 +18,7 @@
<em>grille</em> étant une suite de 81 caractères représentant la grille de gauche à droite puis de haut en bas, soit : <em>grille</em> étant une suite de 81 caractères représentant la grille de gauche à droite puis de haut en bas, soit :
<ul> <ul>
<li>un chiffre entre 1 et 9 pour les cases connues</li> <li>un chiffre entre 1 et 9 pour les cases connues</li>
<li>un point pour les case vides</li> <li>un tiret (-) pour les case vides</li>
</ul> </ul>
Exemple :<br/> Exemple :<br/>
<a href='<?=$newGridUrl?>'><?=$newGridUrl?></a> <a href='<?=$newGridUrl?>'><?=$newGridUrl?></a>

View File

@ -1,5 +1,5 @@
<?php <?php
const UNKNOWN = "."; const UNKNOWN = "-";
$validGrids = array(); $validGrids = array();
@ -27,15 +27,19 @@
class Box { class Box {
public $values = array('1', '2', '3', '4', '5', '6', '7', '8', '9'); public $values = array('1', '2', '3', '4', '5', '6', '7', '8', '9');
public $value = UNKNOWN;
public $rowId;
public $columnId;
public $regionId;
public $candidates;
public $candidateRemoved = array();
public $neighbourhood = array();
function __construct($rowId, $columnId, $regionId) { function __construct($rowId, $columnId, $regionId) {
$this->value = UNKNOWN;
$this->rowId = $rowId; $this->rowId = $rowId;
$this->columnId = $columnId; $this->columnId = $columnId;
$this->regionId = $regionId; $this->regionId = $regionId;
$this->candidates = $this->values; $this->candidates = $this->values;
$this->candidateRemoved = array();
$this->neighbourhood = array();
} }
function searchCandidates() { function searchCandidates() {
@ -48,8 +52,13 @@
} }
class Grid { class Grid {
private $boxes = array();
private $rows;
private $columns;
private $regions;
function __construct($gridStr="") { function __construct($gridStr="") {
$this->boxes = array();
$this->rows = array_fill(0, 9, array()); $this->rows = array_fill(0, 9, array());
$this->columns = array_fill(0, 9, array()); $this->columns = array_fill(0, 9, array());
$this->regions = array_fill(0, 9, array()); $this->regions = array_fill(0, 9, array());

View File

@ -1,19 +1,19 @@
<?php <?php
require("classes.php"); require("classes.php");
global $sudokuGridSolutions;
if (!isset($sudokuGridSolutions)) $sudokuGridSolutions = array();
$fullUrl = $_SERVER["REQUEST_SCHEME"]."://".$_SERVER["HTTP_HOST"].$_SERVER["DOCUMENT_URI"]; $fullUrl = $_SERVER["REQUEST_SCHEME"]."://".$_SERVER["HTTP_HOST"].$_SERVER["DOCUMENT_URI"];
$dirUrl = dirname($fullUrl); $dirUrl = dirname($fullUrl);
$currentGrid = strip_tags($_SERVER['QUERY_STRING']); $currentGrid = strip_tags($_SERVER['QUERY_STRING']);
if (preg_match("/^[1-9.]{81}$/", $currentGrid)) { if (preg_match("/^[1-9-]{81}$/", $currentGrid)) {
if (!array_key_exists($currentGrid, $sudokuGridSolutions)) { session_id($currentGrid);
session_start(["use_cookies" => false]);
if (!array_key_exists("nbSolutions", $_SESSION)) {
$grid = new Grid($currentGrid); $grid = new Grid($currentGrid);
$sudokuGridSolutions[$currentGrid] = $grid->containsDuplicates() ? -1 : $grid->countSolutions(2); $_SESSION["nbSolutions"] = $grid->containsDuplicates() ? -1 : $grid->countSolutions(2);
} }
switch($sudokuGridSolutions[$currentGrid]) { switch($_SESSION["nbSolutions"]) {
case -1: case -1:
$warning = "Cette grille contient des doublons."; $warning = "Cette grille contient des doublons.";
break; break;
@ -27,13 +27,18 @@
} }
require("sudoku.php"); require("sudoku.php");
} else { } else {
$grid = new Grid();
$gridAsString = $grid->toString();
$newGridUrl = "$dirUrl/?$gridAsString";
$sudokuGridSolutions[$gridAsString] = 1;
if ($currentGrid) { if ($currentGrid) {
require("400.php"); require("400.php");
} else { } else {
$grid = new Grid();
$gridAsString = $grid->toString();
$newGridUrl = "$dirUrl/?$gridAsString";
session_id($gridAsString);
session_start(["use_cookies" => false]);
$_SESSION["nbSolutions"] = 1;
header("Location: $newGridUrl"); header("Location: $newGridUrl");
} }
} }