[SOLVED] SESSION scope issue.
Moderator: General Moderators
-
bigbunnyboy
- Forum Newbie
- Posts: 22
- Joined: Sat Mar 12, 2005 5:26 am
Sorry to have a brain the size of an old mushroom that you may find under the fridge after six months, but please can you explain this more fully for me.
I am setting the session vars in the main file, and these session vars are visible to that file when I echo. However, even when set the values are not visible in the include files referenced in the main file.
The reason for this is why? How can I resolve it? Why am i suddenly getting a sinking feeling?
I am so thankful for your time in my request.
I am setting the session vars in the main file, and these session vars are visible to that file when I echo. However, even when set the values are not visible in the include files referenced in the main file.
The reason for this is why? How can I resolve it? Why am i suddenly getting a sinking feeling?
I am so thankful for your time in my request.
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
It's got nothing to do with the included files.
It's a problem with the definition of your session vars.
The only reason that would happen is if the vars haven't been defined (i.e. User not logged in or login script not working).
Can you actually say that if you put
in the main file and also put that exact same code in the login.php file it would echo successfully in the main file but not in login.php?
Steer away from the idea that it has anything to do with your include()'s.
It's a problem with the definition of your session vars.
The only reason that would happen is if the vars haven't been defined (i.e. User not logged in or login script not working).
Can you actually say that if you put
Code: Select all
echo $_SESSION['username'];Steer away from the idea that it has anything to do with your include()'s.
-
bigbunnyboy
- Forum Newbie
- Posts: 22
- Joined: Sat Mar 12, 2005 5:26 am
You have hit the nail on the head!! The main file seems to reference the session vars fine, but the other files are having problems.
I am sure it is staring me in the face, it always is, but for the life of me I cannot see what I am doing wrong. It is so irritating!!
However, $_SESSION vars should be visible throughout the site shouldn't they?
I am sure it is staring me in the face, it always is, but for the life of me I cannot see what I am doing wrong. It is so irritating!!
However, $_SESSION vars should be visible throughout the site shouldn't they?
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
So you're saying that the main file shows them fine? But not the included files?
That can't be right but I'll take your word for it.
You should not need to do this but try it out of mere curiosity....
We're gonna try putting the session id into the arguments for your includes.
I really can't see how, if they are your only notices issued, the sessions vars are available to index.php but nothing included inside it. Can you show the FULL code from login.php? since the top half seems to be missing here and line 21 and 44 aren't what they are in your original file 
EDIT: Correct above code since I missed the concatenating dot
That can't be right but I'll take your word for it.
You should not need to do this but try it out of mere curiosity....
We're gonna try putting the session id into the arguments for your includes.
Code: Select all
<?php include (CSS_TEMPLATE."menu.php?".SID); ?>EDIT: Correct above code since I missed the concatenating dot
-
bigbunnyboy
- Forum Newbie
- Posts: 22
- Joined: Sat Mar 12, 2005 5:26 am
Thanks for the information. As you state things are fine in the main file but not in the included files, the first time that this has happened to me, but then again I hav enot been using PHP for an awfully long time.
I am curious about the code that you have offered to be inserted in my scripts. Do you mean that I should replace the SID string in your example with my session ID (due to the fact that this has not been defined by me, defaults to the PHPSESSID default)?
When I do that the following errors are produced:
This then redirects all mail to:
Any further ideas are most welcome as I am tired of being so incredibly stupid!!
I am curious about the code that you have offered to be inserted in my scripts. Do you mean that I should replace the SID string in your example with my session ID (due to the fact that this has not been defined by me, defaults to the PHPSESSID default)?
When I do that the following errors are produced:
The complete code of the main index file is as follows:Notice: Use of undefined constant PHPSESSID - assumed 'PHPSESSID' in /usr/home/magghill/public_html/test/home.php on line 25
Warning: session_start(): The session id contains invalid characters, valid characters are only a-z, A-Z and 0-9 in /usr/home/magghill/public_html/test/templates/margo_new/login.php on line 2
Warning: Unknown(): The session id contains invalid characters, valid characters are only a-z, A-Z and 0-9 in Unknown on line 0
Warning: Unknown(): Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/tmp) in Unknown on line 0
Code: Select all
<?php
session_start();
function checklogin()
{
include 'includes/config.php';
$table = "member";
$arrmember=array(0=>"member_id",1=>"username",2=>"firstname",3=>"familyname",4=>"email");
$sqlquery = "SELECT member_id,username,firstname,familyname,email FROM member WHERE email = \"".strtolower($_POST["txtMEmail"])."\";";
$arrmember=queryDB($sqlquery,$arrmember,null);
if ($arrmember[0]["members"]>0)
{
$arrmember=array(0=>"member_id",1=>"username",2=>"firstname",3=>"familyname",4=>"email");
$sqlquery = "SELECT member_id,username,firstname,familyname,email FROM member WHERE email = \"".$_POST["txtMEmail"]."\" AND password = \"".crypt($_POST["txtPassword"], CRYPT_STD_DES)."\";";
$arrmember=queryDB($sqlquery,$arrmember,null);
if ($arrmember[0]["members"]>0)
{
$_SESSION['login'] = true;
$_SESSION['id'] = $arrmember[0][0];
$_SESSION['username'] = $arrmember[0][1];
$_SESSION['firstname'] = $arrmember[0][2];
$_SESSION['familyname'] = $arrmember[0][3];
$_SESSION['email'] = $arrmember[0][4];
$sqlupdate="UPDATE member SET logindate=NOW() WHERE member_id=".$_SESSION['id'].";";
alterDB($sqlupdate);
header("Location: ".$_SERVER['HTTP_REFERER']);
}
else
{
$_SESSION['message'] = "Password not recognised. Need a <a href=\"passwordreminder.php\" target=\"_self\" class=\"minibodytext\">reminder</a>?<br>";
displayloginpage ();
}
}
else
{
$_SESSION['message'] = "Email address ".strtolower($_POST["txtMEmail"])." not recognised.<br>";
displayloginpage ();
}
}
function displayloginpage()
{
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>My Title</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="keywords" content="<?php include ('includes/tags.php'); echo $generaltags; ?>">
<meta name="description" content="<?php include ('includes/tags.php'); echo $indexdescription; ?>">
<link href="<?php include ('includes/config.php'); echo CSS_TEMPLATE; ?>fontstyles.css" rel="stylesheet" type="text/css">
</head>
<body>
<table width="100%" border="0" align="center" cellpadding="1" cellspacing="1" id="body" class="bodytext">
<tr>
<td colspan="3" class="menutextinvert">
<?php include (CSS_TEMPLATE."header.php"); ?>
</td>
</tr>
<tr>
<td width="20%" valign="top"> <?php include (CSS_TEMPLATE."menu.php?".PHPSESSID); ?> </td>
<td width="60%" valign="top">
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="minibodytext">
<tr>
<td align="right">home</td>
<td width="5%"> </td>
</tr>
</table>
<br>
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="bodytext">
<tr>
<td colspan="3">
<?php echo "session: ".$_SESSION['username'];
include "includes/includes.php"; include "includes/text.php"; echo LOGIN_WELCOME; ?>
<p> </p></td>
</tr>
<tr>
<td width="10%"> </td>
<td width="80%" valign="top">
<form action="index.php" method="post" enctype="application/x-www-form-urlencoded" name="frmIndex" target="_self" dir="ltr" lang="en">
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="minibodytext">
<tr>
<td class="minibodytextwarning">
<?php if ($_SESSION['message']!=null) { echo $_SESSION['message']."<br>"; $_SESSION['message']=null;} ?>
</td>
</tr>
</table>
</form></td>
<td width="10%"> </td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
</table></td>
<td width="20%" valign="top"><?php include CSS_TEMPLATE."right.php"; ?></td>
</tr>
<tr>
<td colspan="3"><?php include CSS_TEMPLATE."footer.htm"; ?> </td>
</tr>
</table>
</body>
</html>
<?php
}
if ($_SESSION['login']!=null)
{
header("Location: home.php");
}
else
{
include 'includes/functions.php';
if ($_POST["btnSubmit"] == "")
{
displayloginpage();
}
else
{
checklogin();
}
}
?>Code: Select all
<?php
function displaypage()
{
?>
<html>
<head>
<title>My Title</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="<?php include_once ('includes/config.php'); echo CSS_TEMPLATE; ?>fontstyles.css" rel="stylesheet" type="text/css">
</head>
<body>
<table width="100%" border="0" align="center" cellpadding="1" cellspacing="1" id="body" class="bodytext">
<tr>
<td colspan="3"><?php include (CSS_TEMPLATE."header.php"); ?></td>
</tr>
<tr>
<td width="20%" valign="top"><?php include (CSS_TEMPLATE."menu.php?".PHPSESSID); ?></td>
<td width="60%" valign="top">
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="minibodytext">
<tr>
<td align="right"><?php echo "home" ?></td>
<td width="5%"> </td>
</tr>
</table>
<br>
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="bodytext">
<tr>
<td colspan="3"><p>
<?php include ("includes/includes.php"); include ("includes/text.php"); echo LOGGED_WELCOME."<br>".PACKAGE; ?>
</p>
</td>
</tr>
</table></td>
<td width="20%" valign="top"><?php include (CSS_TEMPLATE."right.php"); ?></td>
</tr>
<tr>
<td colspan="3"><?php include (CSS_TEMPLATE."footer.htm"); ?></td>
</tr>
</table>
</body>
</html>
<?php
}
include ("includes/functions.php");
f ($_SESSION["login"]!=null)
{
displaypage();
}
else
{
header("Location: index.php");
}
?>- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
-
bigbunnyboy
- Forum Newbie
- Posts: 22
- Joined: Sat Mar 12, 2005 5:26 am
Sorry, about duplications last night, I was getting tired and my brain only half functions at the best of time.
The login code I posted previously was the complete file, although it was missing the initial <?php tag.
I added the SID value unquoted as requested. The main thing is that when I add it to the menu file that calls login.php, then the file cannot be found, is this significant, or should I have left that include alone?
Sorry to go on about this issue, but you all seem so smart and well..... I am not!! So thank you all agian for your time.
The login code I posted previously was the complete file, although it was missing the initial <?php tag.
I added the SID value unquoted as requested. The main thing is that when I add it to the menu file that calls login.php, then the file cannot be found, is this significant, or should I have left that include alone?
Is this what you meant?Warning: main(login.php?SID): failed to open stream: No such file or directory in /usr/home/magghill/public_html/test/templates/margo_new/menu.php on line 51
Warning: main(login.php?SID): failed to open stream: No such file or directory in /usr/home/magghill/public_html/test/templates/margo_new/menu.php on line 51
Warning: main(): Failed opening 'login.php?SID' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /usr/home/magghill/public_html/test/templates/margo_new/menu.php on line 51
Sorry to go on about this issue, but you all seem so smart and well..... I am not!! So thank you all agian for your time.
-
bigbunnyboy
- Forum Newbie
- Posts: 22
- Joined: Sat Mar 12, 2005 5:26 am
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
-
bigbunnyboy
- Forum Newbie
- Posts: 22
- Joined: Sat Mar 12, 2005 5:26 am
As I mentioned a couple of messages up that I had posted all of the login.php file, itwas only missing the initial <?PHP tag.
Sorry, but the message got lost in among all my other rambling!!
I will repost here however
Sorry, but the message got lost in among all my other rambling!!
I will repost here however
Code: Select all
<?php
function displayloginpage()
{
include "../../includes/includes.php";
include "../../includes/text.php";
?>
<table width="40%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td>
<form action="index.php" method="post" enctype="application/x-www-form-urlencoded" name="frmLogin" target="_self" dir="ltr" lang="en">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td colspan="3" class="minibodytextwarning">
<?php if (isset($_SESSION["message"])) { echo $_SESSION['message']."<br>"; $_SESSION["message"]=null;} ?>
</td>
</tr>
<tr>
<td width="24%" class="smalllogintext"><?php echo $loginstringlabel; ?>:</td>
<td colspan="2" class="smalllogintext"><input type="text" name="txtMEmail" class="smalllogintext" size="20"></td>
</tr>
<tr>
<td class="smalllogintext"><?php echo $loginconfirmlabel; ?>:</td>
<td width="42%" class="smalllogintext"><input type="password" name="txtPassword" class="smalllogintext" size="6"></td>
<td width="34%" class="smalllogintext"><input name="btnSubmit" type="submit" class="smalllogintext" value="<?php echo $submissionbutton; ?>"></td>
</tr>
<tr>
<td colspan="3" class="smalllogintext"><div align="center"><a href="register.php" target="_self" class="smalllogintext">Register</a>.
Password <a href="passwordreminder.php" target="_self" class="smalllogintext">reminder</a>.</div></td>
</tr>
</table>
</form></td>
</tr>
</table>
<?php
}
if (isset($_SESSION['login']))
{
echo " ";
}
else
{
include '../../includes/functions.php';
displayloginpage();
}
?>-
bigbunnyboy
- Forum Newbie
- Posts: 22
- Joined: Sat Mar 12, 2005 5:26 am
I have tried including and omitting with little difference to the visibility of the session vars. It just seems as if they are invisible to the includes, but output quite nicely by the main file.
The same old story really, and I am sorry to drag you guys into my misery. I will shut up soon, before I make you want to kill me too much!!
The same old story really, and I am sorry to drag you guys into my misery. I will shut up soon, before I make you want to kill me too much!!
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
Okay, let's do this.
Main page index.php
And
include.php
Try it out. Does it work?
Main page index.php
Code: Select all
<?php
session_start();
$_SESSION['data'] = "Blob";
include('include.php');
echo "Called from the main page".$_SESSION['data'];
?>include.php
Code: Select all
<?php
echo "Called from the include page".$_SESSION['data'];
?>