Jump to content


Photo

I've never had problems with sessions, but what the heck


  • Please log in to reply
24 replies to this topic

#1 play_

play_
  • Staff Alumni
  • Advanced Member
  • 717 posts

Posted 11 July 2006 - 11:02 PM

Ok.
I've done this alot, and it always worked beautifully, but this time it isnt working.
A log in form, and if all is correct, sessions are started.

if (isset($_POST['submit'])) {
		require ('connection script');
		$username = $_POST['username'];
		$password = $_POST['password'];
		
		$query = "SELECT user_id, username, password FROM table WHERE username = '$username' AND password = '$password'";
		$result = mysql_query($query);
		$row = mysql_fetch_array($result);
		$num = mysql_num_rows($result);
			
		if ( ($num >= 1) && ($row[1] === $username) && ($row[2] === $password) ) {
			session_start();
			$_SESSION['username'] = $username;
			$_SESSION['user_id'] = $row[0];
			header("Location: index.php");
		} else {
			echo 'Log-in failed';
		}
		
	}

I get redirected to index.php.
But in index.php, i try echoing $_SESSION['username'] but i get:"Notice: Undefined index" error
It's probably something minimal im missing, but it isn't working.

Sorry if it's too simple. I've actually been reading books in python lately and my php got rusty.


regex.kat.sh --- regex library
u.kat.sh ---- url shortener
tabbit.org ---- tabbed pastebin

#2 ShogunWarrior

ShogunWarrior
  • Members
  • PipPipPip
  • Advanced Member
  • 528 posts
  • LocationIreland

Posted 11 July 2006 - 11:05 PM

Obvious question: Have you put session_start(); in index.php?
How about using session_register('username','user_id'); and see if that helps.

How about using print_r to see what session is, and take out the redirect for testing.
<a href="http://www.daviddora...nmedia.com/">My New Site/Blog</a> | <a href="http://www.daviddora...m/check/">Check your page for broken links/images/scripts</a>

Zend Certified Engineer
Follow me on Twitter: http://twitter.com/davidd

#3 play_

play_
  • Staff Alumni
  • Advanced Member
  • 717 posts

Posted 11 July 2006 - 11:38 PM

Yes. I have session_start(); in the header.php which is included in index.php.

according to php.net, "Use of session_register() is deprecated" so i try not to use it. But i used it for testing and no luck.

Now, if i take out the header() and echo the session on the same (login) page, it works. like this:
if ( ($num >= 1) && ($row[1] === $username) && ($row[2] === $password) ) {
			$_SESSION['username'] = $username;
			$_SESSION['user_id'] = $row[0];
			echo $_SESSION['username'];
			#header("Location: index.php");
		} else {
			echo 'Log-in failed';
		}


regex.kat.sh --- regex library
u.kat.sh ---- url shortener
tabbit.org ---- tabbed pastebin

#4 ShogunWarrior

ShogunWarrior
  • Members
  • PipPipPip
  • Advanced Member
  • 528 posts
  • LocationIreland

Posted 11 July 2006 - 11:41 PM

So it works on that page, quite strange then.

I hesitate to ask certain things I might check like refreshing index.php etc. maybe a bit of investigation will reveal something.
<a href="http://www.daviddora...nmedia.com/">My New Site/Blog</a> | <a href="http://www.daviddora...m/check/">Check your page for broken links/images/scripts</a>

Zend Certified Engineer
Follow me on Twitter: http://twitter.com/davidd

#5 play_

play_
  • Staff Alumni
  • Advanced Member
  • 717 posts

Posted 11 July 2006 - 11:43 PM

hehe. yes ive tried refreshing.
but still. ive never had this problem, i dont know why its happening now.
regex.kat.sh --- regex library
u.kat.sh ---- url shortener
tabbit.org ---- tabbed pastebin

#6 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 11 July 2006 - 11:44 PM

Please post the header file and index.php

Ken

#7 ShogunWarrior

ShogunWarrior
  • Members
  • PipPipPip
  • Advanced Member
  • 528 posts
  • LocationIreland

Posted 11 July 2006 - 11:46 PM

Hmm.. not sure now, I'd start printing out the session on both ends, testing with a control page etc. but you seem competent enough that you'll be able to seek it out our show some more code, because what you have seems fine.
<a href="http://www.daviddora...nmedia.com/">My New Site/Blog</a> | <a href="http://www.daviddora...m/check/">Check your page for broken links/images/scripts</a>

Zend Certified Engineer
Follow me on Twitter: http://twitter.com/davidd

#8 play_

play_
  • Staff Alumni
  • Advanced Member
  • 717 posts

Posted 11 July 2006 - 11:47 PM

header:
<?php
ob_start();
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>Untitled Document</title>
<link rel="stylesheet" type="text/css" href="includes/pirate.css" />
<script type="text/JavaScript">
<!-- AJAX call -->
var XMLHttpRequestObject = false;

if (window.XMLHttpRequest) {
	XMLHttpRequestObject = new XMLHttpRequest();
} else if (window.ActiveXObject) {
	XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP")
}


function getData(datafile, divID) {
	if (XMLHttpRequestObject) {
		var obj = document.getElementById(divID);
		XMLHttpRequestObject.open("GET", datafile);
		
		XMLHttpRequestObject.onreadystatechange = function() {
			if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) {
				obj.innerHTML = XMLHttpRequestObject.responseText;
			}
		}
		XMLHttpRequestObject.send(null);
	}
}


<!-- Menu RollOver Images
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//-->
</script>
</head>

<body>
	
<table width="100%" cellpadding="0" cellspacing="0" border="0">
	<tr>
		<td class="top" height="136" colspan="10"> header </td>
	</tr>
	
	<tr>
		<td valign="top" class="menu"> <!--menu -->
			<center>
				<div class="menu_header"></div>
				<a href="index.php?content=home" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('home','','images/menu/home_hover.jpg',1)"><img src="images/menu/home.jpg" alt="Home" name="home" width="67" height="26" border="0" id="home" /></a><br />
			 	<a href="index.php?content=news" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('news','','images/menu/news_hover.jpg',1)"><img src="images/menu/news.jpg" name="news" width="55" height="21" border="0" id="news" /></a><br />
			 	<a href="index.php?content=about" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('about','','images/menu/about_hover.jpg',1)"><img src="images/menu/about.jpg" name="about" width="47" height="20" border="0" id="about" /></a><br />
			 	<a href="#" onclick="getData('about.php', 'main_content')" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('calendar','','images/menu/calendar_hover.jpg',1)"><img src="images/menu/calendar.jpg" name="calendar" width="67" height="20" border="0" id="calendar" /></a><br />
			 	<a href="index.php?content=forums" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('forums','','images/menu/forums_hover.jpg',1)"><img src="images/menu/forums.jpg" name="forums" width="59" height="19" border="0" id="forums" /></a><br />
			 	<a href="index.php?content=email" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('email','','images/menu/email_hover.jpg',1)"><img src="images/menu/email.jpg" name="email" width="50" height="19" border="0" id="email" /></a><br />
		  	 	<a href="index.php?content=crew" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('crew','','images/menu/crew_hover.jpg',1)"><img src="images/menu/crew.jpg" name="crew" width="84" height="24" border="0" id="crew" /></a><br />
			 	<a href="index.php?content=extras" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('extra','','images/menu/extrastuff_hover.jpg',1)"><img src="images/menu/extrastuff.jpg" name="extra" width="102" height="20" border="0" id="extra" /></a>
			</center>
			<div class="menu_footer"></div>
		</td><!--/menu -->
		
		<td valign="top">
			<table border="0" width="100%" cellpadding="0" cellspacing="0">
			<tr>
			<td style="width: 82px; height: 65px; background-image: url('images/main content/main_content_top_left.jpg');"></td>
			<td align="left" style="height: 65px; background-image: url('images/main content/top_span.jpg');"></td>
			<td style="width: 82px; height: 65px; background-image: url('images/main content/main_content_top_right.jpg');"></td>
 	 		</tr>
			
			<tr>
			<td style="width: 82px; background-image: url('images/main content/vertical_span_left.jpg');"></td>
			<td id="main_content">



Index:
<?php
require_once('./includes/header.php');
@$id = $_GET['content'];

switch ($id) {
	case 'home':
		include('./home.html');
		break;
		
	case 'news':
		include('./news.php');
		break;
		
	case 'about':
		include('./about.html');
		break;
	
	case 'calendar':
		include('./calendar.html');
		break;
	
	case 'home':
		include('./forums.forums');
		break;
	
	case 'email':
		include('./email.html');
		break;
	
	case 'crew':
		include('./crew.html');
		break;
		
	case 'extras':
		include('./extra.html');
		break;
	
	default:
		include('./home.html');
		break;

}
echo $_SESSION['username'];
require_once('./includes/footer.php');
?>

and i use ob_end_flush() in the footer.
ps: i know the use of tables for layout is lame/looked down on. but trust me, i had to use tables because css was in no way, shape or form cooperating.
regex.kat.sh --- regex library
u.kat.sh ---- url shortener
tabbit.org ---- tabbed pastebin

#9 play_

play_
  • Staff Alumni
  • Advanced Member
  • 717 posts

Posted 11 July 2006 - 11:49 PM

god, i really hope it isnt something too simple that'll make me feel stupid.
regex.kat.sh --- regex library
u.kat.sh ---- url shortener
tabbit.org ---- tabbed pastebin

#10 ShogunWarrior

ShogunWarrior
  • Members
  • PipPipPip
  • Advanced Member
  • 528 posts
  • LocationIreland

Posted 11 July 2006 - 11:52 PM

I think (think) that there may (may) be a conflict between session_start() and ob_start() but that's speculation, plucking at straws here.
<a href="http://www.daviddora...nmedia.com/">My New Site/Blog</a> | <a href="http://www.daviddora...m/check/">Check your page for broken links/images/scripts</a>

Zend Certified Engineer
Follow me on Twitter: http://twitter.com/davidd

#11 play_

play_
  • Staff Alumni
  • Advanced Member
  • 717 posts

Posted 11 July 2006 - 11:54 PM

I think (think) that there may (may) be a conflict between session_start() and ob_start() but that's speculation, plucking at straws here.


I has never given me a problem though. I don't think that is it.

::edit::
I took out ob_start() and still a no-go.
regex.kat.sh --- regex library
u.kat.sh ---- url shortener
tabbit.org ---- tabbed pastebin

#12 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 11 July 2006 - 11:55 PM

Why don't you put the session_start() at the very start of the header.php, that way you don't even need the ob_start().

Ken

#13 play_

play_
  • Staff Alumni
  • Advanced Member
  • 717 posts

Posted 11 July 2006 - 11:58 PM

Why don't you put the session_start() at the very start of the header.php, that way you don't even need the ob_start().

Ken


I actuall just tried that. and took out the header() in the login, and put index.php in the form action. but it is still not working.
regex.kat.sh --- regex library
u.kat.sh ---- url shortener
tabbit.org ---- tabbed pastebin

#14 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 12 July 2006 - 12:05 AM

Putting index.php into the form action won't populate your $_SESSION.

Create another index.php (after saving you're current one) with the following code:
<?php
session_start();
echo '<pre>' .print_r($_SESSION,true). '</pre>';
?>

Use the header() method to get to this file.

If that works (and it should) start adding your code back in and see where it breaks.

Ken

#15 play_

play_
  • Staff Alumni
  • Advanced Member
  • 717 posts

Posted 12 July 2006 - 12:07 AM

Ken,
that gave me this:


Array
(
)



also, im using PHP Version 4.3.10

regex.kat.sh --- regex library
u.kat.sh ---- url shortener
tabbit.org ---- tabbed pastebin

#16 ShogunWarrior

ShogunWarrior
  • Members
  • PipPipPip
  • Advanced Member
  • 528 posts
  • LocationIreland

Posted 12 July 2006 - 12:11 AM

Hmmm, it works for me with these pages, modifications of yours:

Start:
<?php
                $username = 'joan';
                $password = 'tinky';

                if ( $password!=false) {
                        session_start();
                        $_SESSION = array();
                        $_SESSION['username'] = $username;
                        $_SESSION['user_id'] = $password;
                        header("Location: idx.php");
                } else {
                        echo 'Log-in failed';
                }

Index:
<?php

require('header.php');

$id = 'home';

switch ($id) {
        case 'home':
                echo('./home.html');
                break;

        case 'news':
                echo('./news.php');
                break;

        case 'about':
                echo('./about.html');
                break;

        case 'calendar':
                echo('./calendar.html');
                break;

        case 'home':
                echo('./forums.forums');
                break;

        case 'email':
                echo('./email.html');
                break;

        case 'crew':
                echo('./crew.html');
                break;

        case 'extras':
                echo('./extra.html');
                break;

        default:
                echo('./home.html');
                break;

}

echo $_SESSION['username'];

ob_flush();
?>

Header:
<?php
ob_start();
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>Untitled Document</title>
<link rel="stylesheet" type="text/css" href="includes/pirate.css" />
<script type="text/JavaScript">
<!-- AJAX call -->
var XMLHttpRequestObject = false;

if (window.XMLHttpRequest) {
        XMLHttpRequestObject = new XMLHttpRequest();
} else if (window.ActiveXObject) {
        XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP")
}


function getData(datafile, divID) {
        if (XMLHttpRequestObject) {
                var obj = document.getElementById(divID);
                XMLHttpRequestObject.open("GET", datafile);

                XMLHttpRequestObject.onreadystatechange = function() {
                        if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) {
                                obj.innerHTML = XMLHttpRequestObject.responseText;
                        }
                }
                XMLHttpRequestObject.send(null);
        }
}


<!-- Menu RollOver Images
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//-->
</script>
</head>

<body>

<table width="100%" cellpadding="0" cellspacing="0" border="0">
        <tr>
                <td class="top" height="136" colspan="10"> header </td>
        </tr>

        <tr>
                <td valign="top" class="menu"> <!--menu -->
                        <center>
                                <div class="menu_header"></div>
                                <a href="index.php?content=home" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('home','','images/menu/home_hover.jpg',1)"><img src="images/menu/home.jpg" alt="Home" name="home" width="67" height="26" border="0" id="home" /></a><br />
                                 <a href="index.php?content=news" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('news','','images/menu/news_hover.jpg',1)"><img src="images/menu/news.jpg" name="news" width="55" height="21" border="0" id="news" /></a><br />
                                 <a href="index.php?content=about" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('about','','images/menu/about_hover.jpg',1)"><img src="images/menu/about.jpg" name="about" width="47" height="20" border="0" id="about" /></a><br />
                                 <a href="#" onclick="getData('about.php', 'main_content')" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('calendar','','images/menu/calendar_hover.jpg',1)"><img src="images/menu/calendar.jpg" name="calendar" width="67" height="20" border="0" id="calendar" /></a><br />
                                 <a href="index.php?content=forums" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('forums','','images/menu/forums_hover.jpg',1)"><img src="images/menu/forums.jpg" name="forums" width="59" height="19" border="0" id="forums" /></a><br />
                                 <a href="index.php?content=email" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('email','','images/menu/email_hover.jpg',1)"><img src="images/menu/email.jpg" name="email" width="50" height="19" border="0" id="email" /></a><br />
                                   <a href="index.php?content=crew" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('crew','','images/menu/crew_hover.jpg',1)"><img src="images/menu/crew.jpg" name="crew" width="84" height="24" border="0" id="crew" /></a><br />
                                 <a href="index.php?content=extras" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('extra','','images/menu/extrastuff_hover.jpg',1)"><img src="images/menu/extrastuff.jpg" name="extra" width="102" height="20" border="0" id="extra" /></a>
                        </center>
                        <div class="menu_footer"></div>
                </td><!--/menu -->

                <td valign="top">
                        <table border="0" width="100%" cellpadding="0" cellspacing="0">
                        <tr>
                        <td style="width: 82px; height: 65px; background-image: url('images/main content/main_content_top_left.jpg');"></td>
                        <td align="left" style="height: 65px; background-image: url('images/main content/top_span.jpg');"></td>
                        <td style="width: 82px; height: 65px; background-image: url('images/main content/main_content_top_right.jpg');"></td>
                          </tr>

                        <tr>
                        <td style="width: 82px; background-image: url('images/main content/vertical_span_left.jpg');"></td>
                        <td id="main_content">

It will output "joan" on idx.php as it should so because the database stuff is working, that doesn't matter that I changed it, so is there a problem with the persistency of sessions on the server?
<a href="http://www.daviddora...nmedia.com/">My New Site/Blog</a> | <a href="http://www.daviddora...m/check/">Check your page for broken links/images/scripts</a>

Zend Certified Engineer
Follow me on Twitter: http://twitter.com/davidd

#17 play_

play_
  • Staff Alumni
  • Advanced Member
  • 717 posts

Posted 12 July 2006 - 12:15 AM

Thanks.
Maybe there is a problem with the serve and sessions.

regex.kat.sh --- regex library
u.kat.sh ---- url shortener
tabbit.org ---- tabbed pastebin

#18 play_

play_
  • Staff Alumni
  • Advanced Member
  • 717 posts

Posted 12 July 2006 - 12:19 AM

I just uninstalled and reinstalled php/mysql on my pc and its giving me the same error still.
id upload it to my hosting server but its down right now
regex.kat.sh --- regex library
u.kat.sh ---- url shortener
tabbit.org ---- tabbed pastebin

#19 ShogunWarrior

ShogunWarrior
  • Members
  • PipPipPip
  • Advanced Member
  • 528 posts
  • LocationIreland

Posted 12 July 2006 - 12:31 AM

Sorry, quite a predicement you got there.
<a href="http://www.daviddora...nmedia.com/">My New Site/Blog</a> | <a href="http://www.daviddora...m/check/">Check your page for broken links/images/scripts</a>

Zend Certified Engineer
Follow me on Twitter: http://twitter.com/davidd

#20 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 12 July 2006 - 01:20 AM

Try moving the session_start() to the start of the processing script.

Ken




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users