Reputation: 13
In my PHP file, I want to call a function to return a string variable. But I only want to call this function once, not when the page is re-loaded. So I'm using S_SESSION but checking if it is set is not working and is always calling my function.
Code snippet I'm using is below:
<?php
session_start();
include ("include/connection.php");
include ("include/functions.php");
function find_session_word() {
//Open the file.
$myfile = fopen("french.txt", "r") or die("Unable to open dictionary file.");
$f_contents = file("french.txt");
//Get a random line from the file.
$line = $f_contents[array_rand($f_contents) ];
fclose($myfile);
return $line;
}
if (!isset($S_SESSION['session_word'])) {
$S_SESSION['session_word'] = find_session_word();
}
?>
I would expect when I reload the page, the S_SESSION variable is set and the function is not called. But the function is called very time and I get a new word returned.
I have session_start() at the top of the file.
Upvotes: -2
Views: 50
Reputation: 164
PHP sessions are stored in $_SESSION
, not $S_SESSION
[PHP manual].
Upvotes: 4