Page 1 of 3

Sessions Are Killing ME!!!

Posted: Wed Jun 07, 2006 2:52 pm
by a94060
Ok, I may have posted this many times,but i still cannot get sessions to work!!!!!!!!! I have tried almsot all of the ways people have posted back to me,none of them work. This is what i hve so far:

form page

Code: Select all

<?PHP
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Admin Page</title>
<style type="text/css">
<!--
.style1 {font-size: 16px}
.style4 {font-size: 24px}
-->
</style>
</head>

<body>
<h1>
<?PHP

//simple check to see if the person is logged in
if(isset($_SESSION['in']) && $_SESSION['in'] == 1) {
echo '<font color=green>You are logged in,please go <a href="http://avi.aerohostale.com/admin/main.php">Here</a> to see the main page.</font>';
}
else {
?>
</h1>
<div align="center" class="style1">
  <p class="style4"> Admin Login</p>
  <form id="form1" name="form1" method="post" action="login.php">
    <p>Username
      <input name="you" type="text" id="you" />
  </p>
    <p>Password
      <input name="me" type="text" id="me" />
</p>
    <p>
      <input type="submit" name="Submit" value="Submit" />
      <input type="reset" name="Submit2" value="Reset" />
    </p>
  </form>
  <p>&nbsp;</p>
  <div align="center" class="style1"></div>
</div>
<?PHP
}
?>
</body>
</html>
loging checker page

Code: Select all

<?PHP
session_start();
?>

<?PHP
$you = $_POST['you'];
$me = $_POST['me'];
if(($you == '***') && ($me == '***')) {
$_SESSION['in'] = 1;
echo 'You are in,please go <a href="http://avi.aerohostale.com/admin/main.php">Here</a>';
}
else{
header("Location: http://avi.aerohostale.com/admin");
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Logging In</title>
</head>

<body>
</body>
</html>
i am trying to access the session on this page:

Code: Select all

<?PHP
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Main Admin Page</title>
</head>

<body>
<?PHP
print_r($_SESSION);
error_reporting(E_ALL);
if(!isset($_SESSION['in']) || $_SESSION['in'] != 1) {
echo 'You are not logged in,please go to <a href="http://avi.aerohostale.com/admin">Here</a> to login';
}
else {
echo 'Welcome to the admin page. This is the basic layout so far.<br>Please use one of the links below to do what you would like to.';
echo '<br>';
echo '<br>';
echo '<a href="http://avi.aerohostale.com/admin/addoffer.php">Add An Offer</a>';
echo '<br>';
echo '<a href="http://avi.aerohostale.com/admin/tabview.php">View Pending and Completed Offers</a>';
echo '<br>';
echo '<a href="http://avi.aerohostale.com/admin/pay.php">Make a Payment to Someone</a>';
}
?>
</body>
</html>
this is really killing me. please someone give me a way to make this work. print_r tellsme Array() and then gives me my error message.

Posted: Wed Jun 07, 2006 2:58 pm
by jonra
Did you use session_start() on the page that checks for it (the last code you posted)?

Posted: Wed Jun 07, 2006 3:01 pm
by a94060
all of them have session start in them. :P

Posted: Wed Jun 07, 2006 3:04 pm
by jonra
a94060 wrote:all of them have session start in them. :P
lol - Well, then I guess that idea is out :o

I always seem to run into issues with sessions when I try the multiple condition if statements for some reason. I have you tried just going without the || clause on your check?

Posted: Wed Jun 07, 2006 3:06 pm
by a94060
should i try OR? or ill just try one of them? because one post i read said to do both of them with if isset && so that the "session would exist" but that still did not work.

Posted: Wed Jun 07, 2006 3:14 pm
by Christopher
Have you determined that sessions are working at all? For example, two simple pages: 1) start session and set session var, 2) start session and echo same session var.

Posted: Wed Jun 07, 2006 3:20 pm
by a94060
i jsut tested that and on the second page(miraculously) it showed the value i set for the session.But,those original pages i posted still do not work. It is pretty weired,i have error reporting to all and nothing is ocming out. Only thing i get is Array()

Posted: Wed Jun 07, 2006 4:32 pm
by Christopher
OK, now slowly add one thing at a time to those working scripts, checking each step of the way that it still works.

Posted: Wed Jun 07, 2006 4:48 pm
by a94060
ok,great ideaa...it will take time,but ok

Posted: Wed Jun 07, 2006 4:56 pm
by RobertGonzalez
You know that in the code you posted that you are only going to get a session var ($_SESSION['in']) if there is a $_POST value to assign to $you and $me?

Try using a brief session test without interactin with a form at first. I'd bet the issue your having has something to do with the form post data not matching what is set in your if conditional. Just a thought.

Posted: Wed Jun 07, 2006 5:55 pm
by a94060
You all were basically right. After some time,i read into the switch statement and made my script a heck a lot easier. i did it so the sessions would all stay on the same page since i could not pass around. Since i saw the sessions worked on 1 page,i just used it liek that.

This is it so far:

Code: Select all

<?PHP
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Admin PAge</title>
</head>

<body>
<?PHP
/*DEfine All or most variables here*/
$do = $_POST['Submit'];
$you = $_POST['you'];
$me = $_POST['me'];
/*After this is all code*/
switch($do) {
case 'Login'://if the form is submitted
	if(($you == '***') && ($me == '***')) {
	$_SESSION['in'] = 1;
	echo 'Welcome to the admin page. This is the basic layout so far.<br>Please use one of the links below to do what you would 	like to.';
	echo '<br>';
	echo '<br>';
	echo '<a href="http://avi.aerohostale.com/admin/addoffer.php">Add An Offer</a>';
	echo '<br>';
	echo '<a href="http://avi.aerohostale.com/admin/tabview.php">View Pending and Completed Offers</a>';
	echo '<br>';
	echo '<a href="http://avi.aerohostale.com/admin/pay.php">Make a Payment to Someone</a>';
	}//closes the case submit
	break;//stop here if the username and password matches
	default://if none are set
			?>
			<div align="center" class="style1">
  <h1 align="center" class="style4">Admin Login</h1>
  <form id="form1" name="form1" method="post" action="<?PHP echo $_SERVER['PHP_SELF'];?>">
    <p align="center">Username
      <input name="you" type="text" id="you" />
  </p>
    <p align="center">Password
      <input name="me" type="text" id="me" />
</p>
    <p align="center">
      <input type="submit" name="Submit" value="Login" />
      <input type="reset" name="Submit2" value="Reset" />
    </p>
</form>
  <p>&nbsp;</p>
  <div align="center" class="style1"></div>
  <?PHP
	}//closes the switch
	?>
</body>
</html>
Since i had a little of time,i just did this. Thanks all though.I still kinda do have the problem because i think that i i add some more things,it will start to get slower and i will need to make another script.

Posted: Sat Jun 10, 2006 8:20 am
by a94060
jsut an update,i finally solved the problem. I think it was because i was going between directories. If i set a session in the /this directory,it would not work in the /this/is or /this/is/cat directory.

Posted: Sat Jun 10, 2006 2:14 pm
by Chris Corbyn
a94060 wrote:jsut an update,i finally solved the problem. I think it was because i was going between directories. If i set a session in the /this directory,it would not work in the /this/is or /this/is/cat directory.
That doesn't sound right :? If the domain name changes across the pages then it will break however (http://www.sitename.com --> sitename.com).

Posted: Sat Jun 10, 2006 2:46 pm
by a94060
im not sure anymore because it is completly messed up on like everyway i tried it.

Posted: Sat Jun 10, 2006 3:55 pm
by RobertGonzalez
Just a suggestion to see if it is the session rather than the code...

For every session var you plan to set, initialize it to an empty type of what it will be (either array(), 0, or '') before ever approaching it in your script. Then, where you plan on setting the var, set it. After all the checking and setting, echo the var out. If it is the empty type value then you are not setting the session vars right, if the script errors about undefined indexes, then sessions are hosing. The only other possibility is the correct setting of session vars which would render correctly.

Code: Select all

<?php
session_start();
$_SESSION['arrayvar'] = array();
$_SESSION['intvar'] = 0;
$_SESSION['strvar'] = '';

// Run you code for checking and setting
// Then echo out the session var
printr($_SESSION['arrayvar']);
echo $_SESSION['intvar'];
echo $_SESSION['strvar'];
?>