Jump to content

Archived

This topic is now archived and is closed to further replies.

play_

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

Recommended Posts

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.

[code]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';
}

}[/code]

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.

Share this post


Link to post
Share on other sites
Obvious question: Have you put session_start(); in index.php?
How about using [b]session_register('username','user_id');[/b] and see if that helps.

How about using [b]print_r[/b] to see what session is, and take out the redirect for testing.

Share this post


Link to post
Share on other sites
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:
[code]
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';
}[/code]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
hehe. yes ive tried refreshing.
but still. ive never had this problem, i dont know why its happening now.

Share this post


Link to post
Share on other sites
Please post the header file and index.php

Ken

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
header:
[code]
<?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">[/code]



Index:
[code]
<?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');
?>[/code]

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.

Share this post


Link to post
Share on other sites
god, i really hope it isnt something too simple that'll make me feel stupid.

Share this post


Link to post
Share on other sites
I think (think) that there may (may) be a conflict between session_start() and ob_start() but that's speculation, plucking at straws here.

Share this post


Link to post
Share on other sites
[quote author=ShogunWarrior link=topic=100247.msg395489#msg395489 date=1152661923]
I think (think) that there may (may) be a conflict between session_start() and ob_start() but that's speculation, plucking at straws here.
[/quote]

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
[quote author=kenrbnsn link=topic=100247.msg395492#msg395492 date=1152662141]
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
[/quote]

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.

Share this post


Link to post
Share on other sites
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:
[code]<?php
session_start();
echo '<pre>' .print_r($_SESSION,true). '</pre>';
?>[/code]

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

Share this post


Link to post
Share on other sites
Ken,
that gave me this:


Array
(
)



also, im using PHP Version 4.3.10

Share this post


Link to post
Share on other sites
Hmmm, it works for me with these pages, modifications of yours:

Start:
[code]
<?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';
                }
[/code]

Index:
[code]
<?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();
?>
[/code]

Header:
[code]
<?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">
[/code]

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?

Share this post


Link to post
Share on other sites
Thanks.
Maybe there is a problem with the serve and sessions.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Try moving the session_start() to the start of the processing script.

Ken

Share this post


Link to post
Share on other sites
Are you using linux? I am having problems with sessions not even working on my local linux install of Fedora at this moment in time, which is causing no end of hassles since i need to "present" this afternoon. I understand there are some settings somewhere for it, but since no-one is having any joy with your code, maybe it's not the code but other factors at work?

If i get a reply on this from elsewhere (asked on a linux forum about sessions and php) then I'll post what i get told here too - just incase.

Share this post


Link to post
Share on other sites
Im back.
Chips, i am using localhost on windows. But i figured out the problems.
It was my antivirus/malware program. I guess it was stopping the sessions. I turned it off and now it works fine.
Thanks again all.

Share this post


Link to post
Share on other sites
[quote author=ShogunWarrior link=topic=100247.msg395679#msg395679 date=1152701299]
Congrats.
[/quote]

thank you.
who would've ever thought that the antivirus program would interfere with sessions.

Share this post


Link to post
Share on other sites
Yeah, I always presumed that sessions were a given, and that cookies were the thing that were on/off sometimes, hmmm.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.