I wrote code for a web page that uses sessions. I'm using Mac and it is working fine. I tried my page on a Windows, and the sessions don't seem to work. It turns out the session variables I declare do not exist in the other pages. I used session_start() at the beginning of every page I have. One of the solutions I read about this problem was to use session_write_close() but it did not work. When I use var_dump() on $_SESSION I only see the ones declared in the same page (This is the case in both Mac and Windows). On windows and on Mac I used Chrome. And one of the reason I though it worked on Mac was because I didn't get an error, it just worked, however, on Windows, I get the line of error and it said the index of session I'm using is undefined.
What is the problem in my case? And how can I solve it?
EDIT:
Here is the code for setting the variables in session, this is the very first block of the page
<?php
session_start();
session_destroy();
$_SESSION["contestant_name"]["topics_done"] = array( FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE ); // topics done
$_SESSION["contestant_name"]["topics_score_correct"] = array();
$_SESSION["contestant_name"]["topics_score_wrong"] = array();
$_SESSION["contestant_name"]["correct"] = 0;
$_SESSION["contestant_name"]["wrong"] = 0;
?>
And here is code for reading variables
<?php
session_start();
if (isset($_POST["submit"]))
$name = $_POST["name"];
if(!isset($_SESSION["contestant_name"]["name"]))
$_SESSION["contestant_name"]["name"] = $name;
// if ($_SESSION["contestant_name"]["name"] == "")
// $_SESSION["contestant_name"]["name"] = $name;
print $_SESSION["contestant_name"]["name"];
$arr = $_SESSION["contestant_name"]["topics_done"]; // getting it as null or empty
var_dump($_SESSION);
print count($arr);
print_r($arr);
?>
Here is where I am trying to use the session variables after storing them in a previous page.
The code at the top of every page BEFORE anything else should be (make sure it's on the top line as I've put it)
<?php session_start();
ob_start();?>
And at the bottom of every page put:
<?php ob_end_flush();?>
Don't put anything before the first code or after the 2nd code.
Related
I'm trying to set a session variable to record when people have voted but PHP is flat out refusing to set it. The code is as follows:
elseif (isset($_GET['group']) && isset($_GET['vote']))
{
include_once(_INC.'otherheader.php');
groupVotePage($group, $vote);
$_SESSION[$group] = '1';
echo $_SESSION[$group];
}
Nothing. The function groupVotePage adds the vote to the database and echoes a thanks message. $group is the name of the group being voted for. I have session_start(); at the top of the page and have tried to declare the variable inside the function called as well, putting session_start(); everywhere. Session variables are used elsewhere on the site so I know it's not a server issue, and it's the same on all browsers I tried.
Declaring the session var inside the function works but only within the function - it doesn't go global.
if(!isset($_SESSION[$group])) {
$totalVote=$totalVote+$vote;
$totalNumVotes=$totalNumVotes+1;
$totalRating=round($totalVote/$totalNumVotes);
$totalScore=$totalVote*$totalNumVotes;
...db stuff...
$mysqli->query($query);
echo'Thanks for voting!';
}
else {
echo'You have already voted for this group!';
}
Maybe you can use PHP error reporting code to figure out where you going wrong
// Report all errors
error_reporting(E_ALL);
Okay fixed it, the < !DOCTYPE HTML> section was before the session_start();, when I put it after the code worked.
I need to set a dynamic id for every document load. This ID should be set in PHP. Now, when i set a session_start(), the php code would be executed twice because the ID-code in generated document source differs from the same code in the alerted script variable. There are no runtime errors.
What's wrong with this code and how can i prevent the session to re-execute my code after session_start() ?
And when the code would be executed twice (var idvar contains a different value) why the heck is alert executed only once?
I simplified the script so you can try for your self:
<?php
// Session start generates two different ID's
session_start();
// Create ID
$time = microtime(1);
$parts = explode('.', (string)$time);
$idvar = strtoupper(strrev(dechex($parts[0]) . dechex($parts[1]))) . dechex(rand());
?>
<script>
// Contains a new generated ID after session_start()
var IDvr = '<?php print $idvr; ?>';
alert(IDvr);
</script>
Screenshots:
Try move the id initialization just before session_start() i suspect it related on how things executed in php after session_start().
Edit
still unable to reproduce it
I found this:
PHP Session storing behavior into variables?
But even when i add a favicon to the page it don't works. Also the page is standalone and not included as require in another page.
Even this is not working!
// Session start generates two different ID's
if(!isset($_SESSION) && !isset($idvr)) {
session_start();
// Create ID
$time = microtime(1);
$parts = explode('.', (string)$time);
$idvr = strtoupper(strrev(dechex($parts[0]) . dechex($parts[1]))) . dechex(rand());
}
Update:
Now corrected:
With view-source:pgname and session initialized the code would be executed again in source view!
I have code
<?php
require_once("lib/functions.php");
$page = new page;
if(isset($_POST['jmeno'])){
$page->mailit($_POST, $_SESSION['result']);
}
$_SESSION['f'] = rand(1,9);
$_SESSION['s'] = rand(1,9);
$_SESSION['result'] = $_SESSION['f'] + $_SESSION['s'];
?>
Before POST form it prints the right values, but after POST form the sessions are empty. I don't know why, looks like something is bad configured. So it's always show Bad counted result
Here is session configuration....
make sure that this line of code:
session_start();
Is at the beggining of your php file, right after the opening php tag <?php
This line of code is used to start new or resume existing session, please see php manual here
Make sure that your file had UTF-8 encoding. (not UTF-8-BOM)
I have a function on my page to login it works like this:
function entrarSistema($email,$senha){
if(isset($email) and (autentica($email,$senha)!=false)){
$mysqli = connect_db();
$result = mysqli_query($mysqli,"SELECT ID FROM px_user WHERE email = '$email'");
$id = mysqli_fetch_array($result);
$_SESSION['nome'] = autentica($email,$senha);
$_SESSION['email'] = $email;
$_SESSION['password'] = $senha;
$_SESSION['ID'] = $id[0];
$_SESSION['logado'] = true;
}
else{
if(check_double($email)==1){
setCodeAlerta(1);
echo $_SESSION['status'];
}
else {
setCodeAlerta(2);
}
}
}
This function works fine, and the Session variables are set when i call the function setCodeAlerta(), the Session variable i've declared wont work. Notice that this 2 functions are on the SAME file Here is the function:
function setCodeAlerta($numeroCodigo){
$_SESSION['status'] = $numeroCodigo;
}
My index.php has all the pages included, and i use url_rewrite to add the piece of codes that i need, and has this on its very top:
if( !isset($_SESSION) ){ session_start(); }
Oddly enough, if i call directly a file named test.php with this code:
<?php
setCodeAlerta(2);
?>
The variable is set fine, and everything works well.
Thanks in advance.
I'd add a comment, but my rep isn't high enough yet. Out of curiosity are the other session vars available at that point that you are setting? -- I ask because I'm running your code (stripped down) and it's returning OK. I'm just thinking there's something more to this than just that. Are you calling session_start() twice anywhere in an include or anything?
I got that. Everything was absolutely fine with my code, i just did a conditional, where my code would never really go to wrong username/password, hence, not accessing my function (wich is working fine). After removing the second part (after the and) part of this conditional, my code worked just fine.
if (isset($_POST['submitLogin']) and autentica($_POST['emailLogin'],$_POST['passwordLogin']){
entrarSistema($_POST['emailLogin'],$_POST['passwordLogin']);
}
I normally ask questions and find my answer minutes after that, i think thats a bad habit of mine. Even so, i spent the last 5 hours debugging it.
Thanks!
I'm trying to use PHP session variables to carry data over multiple pages. I am using session variables on many parts of my site, but this is the first place where they don't work.
I'm setting it like this:
$_SESSION["savedRetailerName"] = $foo;
And calling it like this:
echo $_SESSION["savedRetailerName"];
The session id remains the same between these two pages, and I'm sure that I'm setting the variables right and that they are being called right. I start the session correctly, and even on that particular page, other session variables are being shown properly.
How can I begin to debug this odd behavior?
Edit:
There are two different pages I'm currently dealing with. Page 2 sets the session variables, and there is a button that will return the user to Page 1. The idea is to still have the fields in Page 1 filled in if the user wishes to return to Page 1.
It is not a cache problem, and I can return other session variables in the exact same spot in my code as where I am failing to return these variables.
The only other code that may be pertinent is the back button handler (jQuery):
$('#backButton').live('click',function() {
window.location.replace("page 1");
});
Edit 2:
I believe this isn't working because of something with variables here:
<?php
$retailerName = $_REQUEST["retailerName"];
$description = $_REQUEST["description"];
$savingsDetails = $_REQUEST["savingsDetails"];
$terms = $_REQUEST["terms"];
$phone = $_REQUEST["phone"];
$address = $_REQUEST["address"];
$zone = $_REQUEST["zone"];
$dateExp = $_REQUEST["dateExp"];
$tag = $_REQUEST["tag"];
$_SESSION["rn"] = $retailerName;
$_SESSION["de"] = $description;
$_SESSION["sd"] = $savingsDetails;
$_SESSION["tm"] = $terms;
$_SESSION["ph"] = $phone;
$_SESSION["ad"] = $address;
$_SESSION["zo"] = $zone;
$_SESSION["ex"] = $dateExp;
$_SESSION["tg"] = $tag;
?>
I am able to set any session variable to a string, but it won't set to a variable.
You want to use session_start before you set or use any session variables. You only need to call it once.
If it's working in other places, odds are that this particular block of code is being executed before session_start is called.
remove all non printable characters before <?php
you may not see them..
You have spaces before your <php tag
You don't have session_start() anywhere
You are using the $_REQUEST variable which is sketchy (use $_GET or $_POST instead)
You would also need to register the session using
session_register # php.net