Me and @bertdasquirt have been working on a neat little project together, and it's partially an exercise to learn OOP as I go, and for the most part, it's been great; until now, that is. I can't seem to create sessions in my aptly-named session class:
<?php
session_start();
class Session {
public function set($name, $value = '', $use_post = false) {
$this->value = $use_post !== false ? $_POST[$value] : $value;
if(is_array($this->value) || is_object($this->value)) {
$this->value = '';
foreach($value as $key => $value) {
$this->value .= $key . ':' . $value . ';';
}
}
if(isset($value) && $value !== '') {
$_SESSION[$name] = $value;
}
return $this;
}
public function get($name) {
return (isset($_SESSION[$name]) ? $_SESSION[$name] : false);
}
public function destroy($what) {
session_destroy($what);
}
}
Which I call like this (in my test case):
<?php
$session = new Session($_POST);
$value = (isset($_POST['val']) ? $_POST['val'] : '');
if($value !== '') {
$session->set('test', $value);
}
var_dump($session);
?>
<form action="" method="post">
<label for="val">Set a session</label>
<input id="val" name="val" value="<?php if(isset($_POST['val'])) echo $_POST['val']; ?>">
<button type="submit" name="submit">Set cookie</button>
</form>
This works great, as long as there's post data. As soon as I dismiss Chrome's "Confirm Form Resubmission" dialog, or force a clean refresh, my session is gone.
I'm genuinely stumped here. Any thoughts or help would be greatly appreciated! :)