1

I am sure this is something really simple but I can't figure out what's wrong.

I have set up my html, php, and js file to work together but am having trouble with setting my own session variable and checking it across files.

I have made sure that both the HTML and PHP files contain the include for my session.php file (the file just handles session_start if not already set).

To sum it up, my HTML file has a function (userSelection aka fxn1). This function passes a value to another function (showGameInfo aka fxn2), and my .js file handles fxn2. Then, fxn2 sends it to the php file which spits out the information to display. Everything works fine until I try to define my own variable. I did this inside fxn1 in the html file. Then, I tried to echo the $_SESSION['test'] value in the php file. This is the line I added to fxn1:

<?php $_SESSION['test'] = 1 ?>;

Am I missing something really simple here? My php file shows the SESSION is set. Please let me know what might be wrong.

FXN1 is in my HTML file like this:

<script>
function userSelection(val) {
    <?php $_SESSION['test'] = 1; ?>
    showGameInfo(val);
}
</script>

If I take out the SESSION['test'] line in the html, the php file simply says the variable test is undefined, and displays the rest as it should.

So that tells me it's wrong in the HTML somehow..but why?

Edit1: I have included session.php in my html and php file. session.php contains this:

<?php 
if (!isset($_SESSION))
    session_start();
?>

Is that incorrect? It's at the top of the html. And this is how it is included in the php file as well, and my php file shows isset($_SESSION) to be true, so I assume it's correct.

Gredenko
  • 13
  • 5

3 Answers3

1

Start your session first using

session_start();

Then only you can work with sessions.

<script>
function userSelection(val) {
    <?php 
     session_start();//starts your session
      $_SESSION['test'] = 1;//sets session variable ?>
    showGameInfo(val);
}
</script>

For more see manual PHP Sessions

Hikmat Sijapati
  • 6,869
  • 1
  • 9
  • 19
1

""FXN1 is in my HTML file like this:" - define that. As in .html file? – Fred -ii-"

"@Fred-ii-, yes it is in my main .html file, defined as so. – Gredenko"

First you need to start the session and for the .html file, change that to .php or instruct your system to treat those as php.

PHP does not parse directives with .html as a default.

You should also check to see if the session array is set with isset().

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • Okay, I think that's the problem. I read just now that doesn't work in html normally and that must be why it breaks. I changed it to a .php file and it worked. But should I always use the .php extension then if I am going to include any php code? Or is there a more 'correct' solution? Thanks Fred. – Gredenko Feb 03 '17 at 16:33
  • @Gredenko You're welcome. If you want to keep using `.html` as an extension, you can instruct your system (if it supports it) to treat those as php. Here's something you can have a look at http://stackoverflow.com/questions/4687208/using-htaccess-to-make-all-html-pages-to-run-as-php-files – Funk Forty Niner Feb 03 '17 at 16:36
0

session_start(); must be on any page needing to define or recall session variables and must be before defining or calling the variables.

From the PHP manual (emphasis added):

session_start() creates a session or resumes the current one based on a session identifier passed via a GET or POST request, or passed via a cookie.

Also, if you want run PHP in an HTML file, you would need to add this to a .htaccess file in your folder

AddType application/x-httpd-php .htm .html
Jay
  • 1
  • 1