<?php
session_name("tasukessid");
ini_set("session.cookie_httponly", "true");
ini_set('session.hash_function', 7);
ini_set('session.hash_bits_per_character', 5);
session_set_cookie_params(60*60*24*7);
session_start();

(isset($_POST['init']) && !empty($_POST['init'])) ? $init=$_POST['init']:returnError("Error");
(isset($_POST['string']) && !empty($_POST['string'])) ? $string=$_POST['string']:returnError("Error");

if($init=="start"){
	$_SESSION["IDENT"] = session_id();
	session_write_close();
}elseif($init=="expire"){
	$_SESSION = array();
	if (isset($_COOKIE["tasukessid"])) {
		setcookie("tasukessid", '', time() - 1, '/');
	}
	session_destroy();
	echo "destroy";
}elseif($init=="load"){
	if (!isset($_COOKIE["tasukessid"])){
		$_SESSION["IDENT"] = session_id();
		session_write_close();
		echo "start";
	}else{
		$_SESSION["IDENT"] = session_id();
		if($_COOKIE["tasukessid"] != $_SESSION["IDENT"]){
			$_SESSION = array();
			setcookie("tasukessid", '', time() - 1, '/');
			session_destroy();
			session_name("tasukessid");
			session_start();
			session_regenerate_id(true);
			$_SESSION["IDENT"] = session_id();
			session_write_close();
			echo "regenerate";
		}else{
			if(isset($_SESSION["primerRes"])){
				echo $_SESSION["primerRes"];
			}else{
				echo "---SPLIT---false";
			}
		}
	}
}elseif($init=="storeP3Res"){
	if (!isset($_COOKIE["tasukessid"])){
		$_SESSION["IDENT"] = session_id();
		session_write_close();
		echo "error---SPLIT---Cookies---SPLIT---true";
	}else{
		$_SESSION["IDENT"] = session_id();
		if($_COOKIE["tasukessid"] != $_SESSION["IDENT"]){
			$_SESSION = array();
			setcookie("tasukessid", '', time() - 1, '/');
			session_destroy();
			session_name("tasukessid");
			session_start();
			session_regenerate_id(true);
			$_SESSION["IDENT"] = session_id();
			$_SESSION["primerRes"] = $string."---SPLIT---true";
			session_write_close();
			echo $_SESSION["primerRes"];
		}else{
			$_SESSION["IDENT"] = session_id();
			$_SESSION["primerRes"] = $string."---SPLIT---true";
			session_write_close();
			echo $_SESSION["primerRes"];
		}
	}
}

function returnError($str){
    echo $str;
}
?>
