Ok, so it seems someone had this issue already, but I don't see how the answer would apply to me. I didn't find any tag misplaced in my coding. My token is set once I arrive on the index.php, but it changes ones it goes to the validate.php. I had it working in the morning but then came back to it to make sure I had it right so I could load it up, I am behind schedule on my work because of this, and now it doesn't work. I had all coding in the same index.php, using if(!isset(session)) set sec_token, but for some reason it stoped working. Then I split it into these files to clear it up, and it still didn't work.
What am I doing wrong, or how can I find the bug to prevent this from happening? -The Token Changing that is- I've tried all other answers, and none worked. I would be pleased if I get some help, Thank you in advance.
PHP session variable changes between pages
//index.php
<?php
session_start();
//in this file I have the random_text(); function nothing else
require_once 'token.php';
//setup a token variable
$token = random_text();
//Session now will have the token
$_SESSION['sec_token'] = $token;
//Now token will be display
echo $_SESSION['sec_token'];
?>
HTML CODE STARTS -->
<!DOCTYPE HTML>
<HTML>
<HEAD>
<title>Token</title>
</HEAD>
<BODY>
<HEADER style="text-align:center;">
<h1>Token</h1>
</HEADER>
<main>
<div style="text-align:center;">
validate.php is where we have the validation of token. -->
<form action="validate.php" method="POST">
<h2>enter what ever</h2>
<input type="text" name="info"><br><br>
<button type="submit" name="test" style="display:inline-block;">Test
Token</button>
<div style="width:2.5%; display:inline-block;"></div>
Here is the hidden input with the token being placed -->
<input type="hidden" name="token" value ="<?php echo $token; ?>">
</form>
</div>
</main>
<FOOTER>
</FOOTER>
</BODY>
</HTML>
//validate.php
<?php
session_start();
//This would echo the token again to make sure stayed the same.
echo $_SESSION['sec_token'];
//Here we set the safety variable $valid = FALSE;
//Here it makes sure that session token is set and the post token
if(isset($_SESSION['sec_token']) && isset($_POST['token']))
{
if($_SESSION['sec_token'] == $_POST['token'])
{
$valid = TRUE;
}
//if false it would destroy sesion, you could skip this if you like.
if($valid == FALSE)
{
//Unset all of the session variables.
session_unset();
If it's desired to kill the session, also delete the session cookie. Note: This will destroy the session, and not just the session data!
if (ini_get("session.use_cookies"))
{
$params = session_get_cookie_params();
setcookie(session_name(), '', time() - 42000,
$params["path"], $params["domain"],
$params["secure"], $params["httponly"]);
}
Finally, destroy the session.
session_destroy();
$_SESSION = array();
exit("<h1>This Web-Site keeps track of IP Address, and attempts
against it!<br> An Alert has been Sent, and it will be investigated!
</h1>");
}
//If True, Then It should do the Transaction!
elseif($valid == TRUE)
{
echo $_POST['info'].'<br>';
echo 'It Worked!'
}
}
}