why use php session with Laravel helpers? your code makes no sense?
Mar 31, 2024
5
Level 1
Sessions help
I'm having a hard time trying to work with sessions. Why are my sessions being reset here?
I want to be able to change the session variables between pages and for them to stay constant but they reset every time i click the link. I have two pages, session1.php and session2.php.
Once the session is created, my IF session_status should prevent the reassignment of the randomNumber, or am I wrong?
My code:
<?php
if (session_status() === PHP_SESSION_NONE) {
session_start();
$_SESSION["randomNumber"] = random_int(1, 100);
session_write_close();
}
echo "<a href='session2.php'>Link to Session2</a><br>";
echo "this is session1.php";
echo '<br>';
echo $_SESSION["randomNumber"];
-----------------------------------------------
<?php
if (session_status() === PHP_SESSION_NONE) {
session_start();
$_SESSION["randomNumber"] = random_int(1, 100);
session_write_close();
}
echo "<a href='session1.php'>Link to Session1</a><br>";
echo "this is session2.php";
echo '<br>';
echo $_SESSION["randomNumber"];
Level 29
Use isset to check if $_SESSION['randomNumber'] exists
if (!isset($_SESSION["randomNumber"])) {
$_SESSION["randomNumber"] = random_int(1, 100);
}
Once you start using the Laravel framework you can use the Laravel session helper https://laravel.com/docs/11.x/session
Please or to participate in this conversation.