0

I am displaying some information based upon the users day of the week. [[Getting the server side day of the week can display the wrong day's information depending on the timezone difference.]] The problem is, with this code, the session variable is not getting set on the first page load. Every next page load, it is set.

The user is visiting the home page and there is no login required.

I would like to be able to get the UserDay variable before the page is loaded, or shortly after it is loaded so that the user does not have to see the wrong information the first time around.

session.php (Main Page)

 session_start();
 $userday= $_SESSION['userday'];    

document.ready (loaded in header)

$(document).ready(function() {
 var now = new Date();          
        var days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
        var UserDay = days[ now.getDay() ];
        $.ajax({
            type: "GET",
            url: "userday.php",
            data: 'userday='+ UserDay,
            success: function(){
            }
        });
});

userday.php

<?php
session_start();

$_SESSION['userday'] = $_GET['userday'];
?>
Mike
  • 29
  • 4
  • why do you make an array of week days in Javascript ? – David Demetradze May 18 '15 at 20:40
  • Not possible. Use client-side code instead maybe, bypassing the need to know the user date on the server. – Sergiu Paraschiv May 18 '15 at 20:40
  • Javascript runs in the browser when the page is sent to the client. So obviously the variable can't be set in the script that's sending the JS to the client, because it runs first. – Barmar May 18 '15 at 20:40
  • Although you're just trying to get the day of week, the solution is the same as for getting the timezone. As in the linked question, you have to force a reload after setting the session variable. – Barmar May 18 '15 at 20:43

0 Answers0