Home > Blockchain >  PHP- Session lost upon refresh or revist
PHP- Session lost upon refresh or revist

Time:11-02

I'm new to php as you can tell and I'm having trouble with my session.Session is being lost upon refreshing page. I have a simple button that toggles between light and dark mode, it works and I store the value into a session value. I'm able to print the value and see that it is being stored, however upon a refresh or a revisit, the mode switches to the opposite mode and then upon refresh/revisit, the state is actually saved. I've tried to search for my issue but I cant find anything and I'm off to bed so I thought I may as well post in the meantime. I'm not sure what I'm missing, probably something obvious, but I appreciate the help. I can only use PHP for this by the way.

<?php
session_start();
//header('Refresh: 3000; url=index.php');
echo "" . $_SESSION['color'] . "";
    
    echo "<body style = 'background-color: " . $_SESSION['color'] . ";'>";
  
?>
<!DOCTYPE html>
<html lang = "en-US">
<head>
    <link rel="stylesheet" href="index.css">
    <title>
        Web Technologies
    </title>
</head>

<body>

<?php session_start();

include_once "templateFunctions.php";

if (isset($_POST['dark'])) {
    
    if (!isset($_SESSION['color'])) {
        //session_register('color');
        $_SESSION['color'] = "rgb(54, 53, 53)";
        echo "<body style = 'background-color: " . $_SESSION['color'] . ";'>";
        
    } else {
        
        if ($_SESSION['color'] == "rgb(54, 53, 53)") {
            $_SESSION['color'] = "white";
            echo "<body style = 'background-color: " . $_SESSION['color'] . ";'>";
        } else {
            $_SESSION['color'] = "rgb(54, 53, 53)";
            echo "<body style = 'background-color: " . $_SESSION['color'] . ";'>";
        }  
    }
}

/*$color = "rgb(48, 48, 48)";
    
setcookie('color', $color, time()   10, '/');
$_COOKIE['color'] = [$color];
echo "<p>'$cookie'</p>";
echo "<body style = 'background-color: $cookie;'>";

else {
$_SESSION['color'] = "rgb(54, 53, 53)";

echo "<body style = 'background-color: " . $_SESSION['color'] . ";'>";

}
*/


?>
<div class = "header">
    <div class = "title" >
        <h1>Jimbo Fimbo</h1><h2><u>Software Stuff</u></h2></th>    
    </div>

    <div class = "img">
        <img id = "img1" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTci5Mqm2mgMe_9KfJR0TqMEd-A_wtmqq69cru0wed7OEQF6jVAYycqCY_KzWV0o3hIVYs&usqp=CAU" alt="https://i.ytimg.com/vi/KEkrWRHCDQU/maxresdefault.jpg">
    </div>
</div>
    
    
<hr style = "margin-bottom: 20px;">
<div class = "colgroup">
    <div >
        <h3 style = "text-align: center;">Menu</h3><hr style = "color:white;">
        <ul style = "padding-left:20px; font-size: 25px;">
            <li><a href="https://github.com">GitHub<br></a></li>
            <li><a href="courses.html">Courses<br></a></li>
            <li><a href="https://google.com">School<br></a></li>
        </ul>
    </div>    
        
    <div >
        <h1>About Me</h1>
        
        <img id = "img2"src="https://i.ytimg.com/vi/KEkrWRHCDQU/maxresdefault.jpg">
        <p> 
             Heres some lorem ipsum! <br> Lorem ipsum dolor sit amet. Reprehenderit sunt est quia 
            necessitatibus est eius quis. Est dolor adipisci et dolor molestiae hic vitae expedita eum inventore quam aut mollitia natus. Qui quia 
            dolor aut totam Quis qui expedita repudiandae non quam magni et enim ipsa qui consequatur omnis. Ea incidunt debitis est nemo nesciunt 
            eum quia rerum eum recusandae sunt nam maiores saepe. Hic omnis dolores ab deserunt vero cum fugiat explicabo vel perferendis numquam. 
            Sed unde voluptatibus quo aliquid iure rem accusamus voluptatum aut maxime adipisci id molestiae voluptatem? In quia necessitatibus et 
            provident id nobis eius ea enim voluptatem in aliquam voluptas ut similique facilis. Et porro ipsam eos excepturi voluptatem non ullam sint. 
         </p>
         <p>Stuff goes here </p>
        
    </div>
    
    
    <div >
        <p style = "text-align: center;">Enrolled Courses</p>
        <hr style = "color:white;">
        <ol style = "padding-left: 20px; font-size: 25px;">
            <li>CS-3753</li>
            <li>CS-4393</li>
            <li>CS-4413</li>
            <li>CS-4423</li>
            <li>CS-4843</li>
        </ol>

        <form action="index.php" method = "post" accept-charset=utf-8 >
            <input type="submit" name = "dark" id ="submit" value = "Dark Mode">
        </form>
    </div>
</div>

<footer >
Copyright 2022, Jimbo Fimbo
</footer>
</body>
</html>

I'm expecting what I mentioned above. I've looked over the code to no avail, I've tried using another session variable to save the color change, I tried saving the session to a path using said method, and I've looked on the internet for similar problems but I guess I suck at googling since this seems like it would be straightforward.

CodePudding user response:

If you want to get rid of the problem that the session variable will be "toggled" on page refresh, One of the ways is to separate the change color part from your display part, so the form submission target can be set to "changecolor.php", which will trigger a page redirection to index.php upon changing the session variable :

index.php

<?php
session_start();

/// initalize the color to be white if no color session variable
if (!isset($_SESSION['color'])) {
  $_SESSION['color'] = "white";
}

/// other HTML code

<form action="changecolor.php" method = "post" accept-charset=utf-8 >
   <input type="submit" name = "dark" id ="submit" value = "Change Color Mode">
</form>

changecolor.php

<?php session_start();

if (isset($_POST['dark'])) {
    
    if (!isset($_SESSION['color'])) {
        $_SESSION['color'] = "rgb(54, 53, 53)";
     echo "<body style = 'background-color: " . $_SESSION['color'] . ";'>";
        
    } else {
        
        if ($_SESSION['color'] == "rgb(54, 53, 53)") {
            $_SESSION['color'] = "white";
            echo "<body style = 'background-color: " . $_SESSION['color'] . ";'>";
        } else {
            $_SESSION['color'] = "rgb(54, 53, 53)";
            echo "<body style = 'background-color: " . $_SESSION['color'] . ";'>";
        }
        
    }

header("Location: index.php");

}
?>
  • Related