...

View Full Version : Unable to set session variable



withoutnick
09-13-2011, 10:38 AM
Hello everyone,

I'm stucked with a very little piece of code.



session_start();
$connection = mysql_connect('localhost', 'user', 'pass');
mysql_select_db('database');
$query = "select * from table where `code` = '" . $_GET['kodas'] . "' && `used` = '0'";
$result = mysql_query($query);
if(mysql_num_rows($result) !== '0') {
$_SESSION['code'] = "" . $_GET['kodas'] . "";
}

The problem is that if I use this piece of code session variable is being set only for one page. After refreshing it disapears. If I create the session variable directly (without checking for data in database, everything goes fine.

I would be grateful for any help!

mlseim
09-13-2011, 12:23 PM
On every script that uses the SESSION variable, you need this at the top:

<?php
session_start();

Do your other scripts have that?


.

withoutnick
09-13-2011, 12:42 PM
Everything is fine with that session_start();, cause all the action is going on one page.

The structure of my page is like that:



<?
session_start();
if(!isset($_SESSION['code'])) {
// some actions to take to get $_SESSION['code'] set.
}
else {
//showing html form.
}
?>


After $_SESSION variable is set I reload the page. And that reload should show me the HTML form (else part of script), but nothing happens, because for some reason session variable gets lost. Everything works fine if I don't check the database for accuracy of the code. (The code of this check is in the first post of topic.

mlseim
09-13-2011, 01:03 PM
Put the SESSION setting part in it's own script
and after running it, redirect back. See if that works.

sess.php


<?php
session_start();
$connection = mysql_connect('localhost', 'user', 'pass');
mysql_select_db('database');
$query = "select * from table where `code` = '" . $_GET['kodas'] . "' && `used` = '0'";
$result = mysql_query($query);
if(mysql_num_rows($result) !== '0') {
$_SESSION['code'] = "" . $_GET['kodas'] . "";
}
header ("location: index.php");
?>






<?php
session_start();
if(isset($_SESSION['code'])) {
// do nothing ... just drop down to the rest of the script.
}
else {
// go set the session
header ("location: sess.php");
}
?>

<html>
Rest of the HTML
</html>





Edit:
Or I got it backwards ...
anyhow, the idea is to create an extra header.
Maybe the SESSION is getting dropped by not getting it sent in the header.
The redirect is transparent to the user, but creates the header.


.

tangoforce
09-13-2011, 01:35 PM
Have you tried performing a var_dump($_SESSION) to see whats going on?

withoutnick
09-14-2011, 09:35 AM
I found out that all the problem is caused by database selection line. No matter what, if I select database, session lasts only for one page. Can it be bacouse of my local server configuration?

tangoforce
09-14-2011, 01:15 PM
It shouldn't be.

Fou-Lu
09-14-2011, 02:46 PM
This is always true regardless of what the results are:


if(mysql_num_rows($result) !== '0') {

Mysql_num_rows returns an integer, not a string. That should be != not !==.
So depending on how this is used depends on if you will always have the session variable overwritten.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum