Page 2 of 4

Posted: Sat Mar 12, 2005 10:04 am
by bigbunnyboy
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.

Posted: Sat Mar 12, 2005 10:45 am
by Chris Corbyn
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

Code: Select all

echo $_SESSION['username'];
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.

Posted: Sat Mar 12, 2005 11:34 am
by bigbunnyboy
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?

Posted: Sat Mar 12, 2005 2:23 pm
by Chris Corbyn
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.

Code: Select all

<?php include (CSS_TEMPLATE."menu.php?".SID); ?>
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

Posted: Sat Mar 12, 2005 5:09 pm
by bigbunnyboy
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:
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
The complete code of the main index file is as follows:

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%">&nbsp;</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>&nbsp;</p></td>
        </tr>
        <tr> 
          <td width="10%">&nbsp;</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%">&nbsp;</td>
        </tr>
        <tr> 
          <td colspan="2">&nbsp;</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();
  }
}
?>
This then redirects all mail to:

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%">&nbsp;</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");
}
?>
Any further ideas are most welcome as I am tired of being so incredibly stupid!!

Posted: Sat Mar 12, 2005 5:12 pm
by Chris Corbyn
Unquoted SID in php is itself the session id.

I just mean append it like in the example I gave to all of your include files. It SHOULDN'T make any difference but this is just to kill my curiosity.

Posted: Sat Mar 12, 2005 5:16 pm
by Chris Corbyn
I believe you already posted index.php... we need the code from login.php (in full since you didn't post the top half before).

Posted: Sun Mar 13, 2005 12:04 am
by bigbunnyboy
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?
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
Is this what you meant?
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.

Posted: Sun Mar 13, 2005 3:43 am
by feyd
include() is a file system level function. Unless you are requesting a "remote" file, a query string will not work.

Posted: Sun Mar 13, 2005 3:06 pm
by bigbunnyboy
Okay, so that being the case, have ou any idea why I can view session vars in my main file while not in the include files called by the main file?

Posted: Sun Mar 13, 2005 3:29 pm
by Chris Corbyn
*cough* cough* Last time I'll ask.

Can you please post the FULL code from login.php since you didn't post the top half of it before? :lol:

Posted: Sun Mar 13, 2005 3:49 pm
by bigbunnyboy
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

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 "&nbsp;";
}
else
{
  include '../../includes/functions.php';

  displayloginpage();
}
?>

Posted: Sun Mar 13, 2005 3:58 pm
by feyd
I don't see where login.php is included anywhere in the "main index" you posted early on this page. So if login.php is called without being included by the main index, i.e. called directly, you need session_start() in that script.

Posted: Sun Mar 13, 2005 4:17 pm
by bigbunnyboy
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!!

Posted: Sun Mar 13, 2005 4:21 pm
by Ambush Commander
Okay, let's do this.

Main page index.php

Code: Select all

<?php
session_start();

$_SESSION['data'] = "Blob";

include('include.php');

echo "Called from the main page".$_SESSION['data'];
?>
And

include.php

Code: Select all

<?php
echo "Called from the include page".$_SESSION['data'];
?>
Try it out. Does it work?