/    Sign up×
Community /Pin to ProfileBookmark

Session Problem

My sessions are not being sent across my documents and I cannot understand why. I have the session_start() function at the top of all of my documents.

Here is the submission code:

[code=php]
$_SESSION[‘errors’]=$INVALID;
header(“Location: ../edit.php?id=$ID&table=$TABLE&connection=$CONNECTION”);[/code]

and then on the form document when do this:

[code=php]echo $_SESSION[‘errors’];[/code]

nothing appears. I have fiddled and fiddled but cannot work out why. Any suggestions?

Thank you

to post a comment
PHP

13 Comments(s)

Copy linkTweet thisAlerts:
@ScottyBoyJan 11.2011 — Are you using session_start(); before you try to access the variable? For instance...

[code=php]
<?php
session_start();

$INVALID = 'something';

$_SESSION['errors'] = $INVALID;

?>
[/code]


And

[code=php]
<?php
session_start();

echo $_SESSION['errors'];
?>
[/code]
Copy linkTweet thisAlerts:
@Benji6996authorJan 11.2011 — Of course. I have worked with sessions a fair bit in the past so I am pretty sure I am not missing something. This is really confusing!!
Copy linkTweet thisAlerts:
@ScottyBoyJan 12.2011 — I'm a bit confused myself. Assuming that $INVALID does have a value, I don't see why it wouldn't work.
Copy linkTweet thisAlerts:
@DasherJan 12.2011 — Oops never mind -- brain freeze
Copy linkTweet thisAlerts:
@ScottyBoyJan 12.2011 — It should, but it appears that $_SESSION['errors'] has no value. Try this, and tell us what it comes back with. I have a feeling your original assignment variable ($INVALID) had no value, and thus couldn't add anything to the session array.

[code=php]
echo'<pre>', print_r($_SESSION), '</pre>';
[/code]
Copy linkTweet thisAlerts:
@Benji6996authorJan 12.2011 — Yes you are as baffled as me. I have done that and if I take away the 'header(Location...', as in I do not redirect the page then it prints. Otherwise it just goes and redirects.

this works:
[code=php]echo $_SESSION['errors'];[/code]

but as soon as I redirect:
[code=php]echo $_SESSION['errors'];
header("Location: ...");[/code]


It redirects before it echos anything. I would have expected it to show an error of 'headers already sent' but apparently not!! The Session ID is changing from page to page, could this have something to do with it? If so please suggest why!?

Thank you
Copy linkTweet thisAlerts:
@criterion9Jan 12.2011 — Could we see the code beginning with session_start()? It sounds like either there is an incorrect setting at the server level or something is altering the session data/storage before you get to the assignment.
Copy linkTweet thisAlerts:
@Benji6996authorJan 12.2011 — Of course you can Criterion9. I will just gather it now and post it in a few mins. Would you like me to also post the session phpinfo()?

Thank you
Copy linkTweet thisAlerts:
@Benji6996authorJan 12.2011 — This is my entire script for this page. There is nothing else on it.
[code=php]<?php

if(!isset($_SESSION)){
session_start();
}

require_once('../_connections/sineCMS.php');
require_once('../_connections/domain.php');
require('SQLFunctions.php');
require('functions.php');
require('validate.php');
require('VARIABLES.php');
include('tiny_mce/plugins/phpimage/classes/class.upload.php');

$UPDATEDATA=array();
$INVALID=array();

function gatherUpdateData($dbfield,$dbtype,$length,$flag,$database,$connection){
global $UPDATEDATA;
global $INVALID;
global $_DATABASES;
global $_CONNECTIONS;
$type=generateType($dbfield,$dbtype,$length,$flag);
$field=preg_replace('/([^:]*:([^)]+))/','',$type[0]);
$param=$type[2];
$type=$type[1];

switch($type){
case 'string':
$_POST[$field]=htmlentities($_POST[$field]);
case 'real':
case 'int':
case 'blob':
case 'date':
case 'select':
$value=$_POST[$field];
break;
case 'multiselect':
$data=array();
foreach($_POST[$field] as $key=>$value){
$data[]=$value;
}
$value=implode(',',$data);
break;
case 'checkbox':
$data=array();
if(isset($_POST[$field])){
foreach($_POST[$field] as $key=>$value){
$data[]=$value;
}
$value=implode(',',$data);
}
break;
case 'image':
if(!empty($_FILES[$field]['name'])){
$value=uploadImage($field);
}else{
$value="images/".$_TABLES[$_SINEMACULA['data']['databaseRef']][$_POST['TABLE']]."/default.png";
}
break;
}
if($type!='bool'){
if(validateElement($value,$field,$dbtype,$flag,$_DATABASES['SINE'],$_CONNECTIONS['SINE'])){
$UPDATEDATA[$dbfield]=$value;
}else{
$INVALID[]=$field;
}
}
}

if(isset($_POST['ID'])&&isset($_POST['TABLE'])&&isset($_POST['CONNECTION'])){
$ID=$_POST['ID'];
$WHERE=array('id'=>$ID);
$TABLE=$_POST['TABLE'];
$CONNECTION=$_POST['CONNECTION'];
$PAGE=$_POST['PAGE'];
$KEY=$_POST['KEY'];
if(!isset($ID)||!isset($TABLE)||!isset($CONNECTION)){header("Location: ../index.php");}
if(empty($_TABLES[$CONNECTION][$TABLE])){header("Location: ../index.php");}
$_DATALIST=selectSQL($_TABLES[$CONNECTION][$TABLE],NULL,array('id'=>$ID),NULL,$_DATABASES[$CONNECTION],$_CONNECTIONS[$CONNECTION],NULL);
$_DATALIST['data']=$_DATALIST['data'][0];
if(empty($_DATALIST['data'])){header("Location: ../index.php");}
$_SESSION['errors']=array();
$i=0;
foreach($_DATALIST['data'] as $key => $value){
if($key!='id'){
gatherUpdateData($key,$_DATALIST['types'][$i],$_DATALIST['lengths'][$i],$_DATALIST['flags'][$i][0],$_DATABASES[$CONNECTION],$_CONNECTIONS[$CONNECTION]);
}
$i++;
}
if(empty($INVALID)){
$update=updateSQL($_TABLES[$CONNECTION][$TABLE],$UPDATEDATA,$WHERE,$_DATABASES[$CONNECTION],$_CONNECTIONS[$CONNECTION]);
if($update=='success'){
header("Location: ../data-list.php?page=$PAGE&key=$KEY");
}else{
$_SESSION['errors'][]='serverError';
header("Location: ../edit.php?id=$ID&table=$TABLE&connection=$CONNECTION");
}
}else{
$_SESSION['errors']=$INVALID;
header("Location: ../edit.php?id=$ID&table=$TABLE&connection=$CONNECTION");
}
}else{
$f=$_GET['field'];
$v=$_GET['value'];
$flag=$_GET['flag'];
$w=array('id'=>$_GET['where']);
$t=$_TABLES[$_GET['connection']][$_GET['table']];
$d=$_DATABASES[$_GET['connection']];
$c=$_CONNECTIONS[$_GET['connection']];

if(validateElement($v,$f,NULL,$flag,$_DATABASES['SINE'],$_CONNECTIONS['SINE'])){
echo updateSQL($t,array($f=>$v),$w,$d,$c);
}
}

?>[/code]
Copy linkTweet thisAlerts:
@Benji6996authorJan 12.2011 — I think I have located the problem. In my phpinfo, the session.cookie_domain is set to the wrong domain! However I have not set this using 'session_set_cookie_params' so I do not know how it has managed to get there. Very confusing!!!! It is not set in my php.ini, it is only the local domain so I am very confused. Any suggestions?

I will probably just have to resort to using 'session_set_cookie_params' to set it to what it is meant to be. Although it would be comforting to know what is making it do this!

Thank you
Copy linkTweet thisAlerts:
@criterion9Jan 12.2011 — You might need to employ a method to set your session parameters then. I was going to ask if you had another script that uses sessions that is working on the same box.
Copy linkTweet thisAlerts:
@Benji6996authorJan 12.2011 — I have done and that has sorted it. It is just very irritating that I do not know what is setting the the domain to the wrong value! Are there any other methods than using 'session_set_cookie_params' to change the session params. Only because I will do a search for it in my code and will hopefully find the problem. I have already checked and there is no other use of 'session_set_cookie_params' in my code so it must be another method.
Copy linkTweet thisAlerts:
@Benji6996authorJan 12.2011 — Found it! It was in my htaccess file. Really sorry to waste your time. Thanks for your help anyway.
×

Success!

Help @Benji6996 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...