Something weird with sessions happening

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pinehead18
Forum Contributor
Posts: 329
Joined: Thu Jul 31, 2003 9:20 pm

Something weird with sessions happening

Post by pinehead18 »

I have a user based site using session. It is acting really weird when i first log in using sessions it takes me to mypage which is a page that you get when first logged in. Then when i click on another page that requires sessions it goes back to the login page and asks me to login again so i do. Then everythign works fine from that point on until i close my browser or log out and have to log back in.

Any ideas?

thank you
Anthony
Deemo
Forum Contributor
Posts: 418
Joined: Sun Jan 18, 2004 11:48 am
Location: Washington DC

Post by Deemo »

sessions expire when the browser closes. if you would like to continue being logged in, use cookies
pinehead18
Forum Contributor
Posts: 329
Joined: Thu Jul 31, 2003 9:20 pm

Post by pinehead18 »

Like i said above, i never close the browser. It logs me in, i click on a link that requires sessions and it takes me back to the login page. So i login one more time and then i'm good to go UNTIL the session expires which is normal.

Any input?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it doesn't require cookies... where in the page is the session started? i.e. Is it started in a frame/iframe? Is it started on each frame/iframe?
Deemo
Forum Contributor
Posts: 418
Joined: Sun Jan 18, 2004 11:48 am
Location: Washington DC

Post by Deemo »

oh sorry, i misread :?
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post by Wayne »

is there any difference in the way you are calling the pages. ie. is one called by IP address and the other by a dns url or are the pages mypage and the other pages mentioned on different urls?
Sat
Forum Newbie
Posts: 5
Joined: Tue Jul 13, 2004 6:58 am
Location: Russia

Post by Sat »

Usually such errors appears in case of overwrite session variables. I think you have to check all your operations with your session variables which controls auth on your site...
pinehead18
Forum Contributor
Posts: 329
Joined: Thu Jul 31, 2003 9:20 pm

Post by pinehead18 »

Even though, after i login once more time, everything works fine after that? But its the first page i view after the first time i login everytime. It is anypage that requires sessions. But after i log back in, any of the pages that did it before, work fine.
pinehead18
Forum Contributor
Posts: 329
Joined: Thu Jul 31, 2003 9:20 pm

Post by pinehead18 »

My sessions are called first thing on the page.

sat: what do you mean an over right? See after i login for the 2nd time, i can view any page and never have a problem again. But i always have to login twice. I know for 100% fact it sets the session even the second time. But get this. It only requires you to login again if your this is the first time your the browser that is open has been on the page. else it always works even after the user logs out.

Any other ideas? Here is my login code..
And here is the code that is called on the top of the first page it views (mypage)

Code: Select all

<?php
$con = mysql_connect("localhost","pinehead","") or die(mysql_error());
$db = mysql_select_db("pinehead",$con) or die(mysql_error());
$lastlogon = date("l, M, d");
$pass      = md5($pass);
$sql       = "SELECT * FROM users where user='$uname' and pass='$pass'";
$result    =  mysql_query($sql);
$row       =  mysql_fetch_array($result);
if ($row["user"] == $uname && $row["pass"] == $pass) {
   $lastlogon = date("l, M, d");

        session_start();
        $_SESSION['name'] = $_POST['uname'];

if(isset($_SESSION['name'])) {
      $sqll = "UPDATE users SET last_logon='$lastlogon' WHERE user='".$_POST['uname']."'";
      if(mysql_query($sqll)) {
       header("location: http://www.kc.com/mypage");
        }
      else {
         echo "Couldn't update user<br />".$sqll."<br />".mysql_error()."";
      }
   }
   else {
      echo "Couldn't Set User Session";
   }
}
else {
        $output = "<center><font face=arial size=-1 color=red>Username/Password do not match <a
href=loginbox.php>Click here to login again</a></font></center>";
        include('html.inc.php');
        start_header($output);

}


?>
and this is the code called at the top of mypage

Code: Select all

<?php

require('html.inc.php');
user_check();

and user check is located in html.inc.php and is as follows

function user_check() {
        if(!isset($_SESSION['name'])) { header("Location: http://www..com/loginbox.php"); exit; }



?>
Thank you again
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

<?php 

require('html.inc.php'); 
user_check(); 

and user check is located in html.inc.php and is as follows 

function user_check() { 
        if(!isset($_SESSION['name'])) { header("Location: http://www..com/loginbox.php"); exit; } 

?>
should be

Code: Select all

<?php 


//and user check is located in html.inc.php and is as follows 

function user_check() { 
        if(!isset($_SESSION['name'])) { header("Location: http://www..com/loginbox.php"); exit; } 

require('html.inc.php'); 
user_check(); 


?>
I BELIEVE because you have not declare what user_check() is until after it is called. Not too sure on this. But this would explain why it only works on the 2nd refresh
pinehead18
Forum Contributor
Posts: 329
Joined: Thu Jul 31, 2003 9:20 pm

Post by pinehead18 »

Thank you for you rhelp but unfortunantly it does not work . It has always been called after it was delcared.

Any other ideas?
thank you
anthony
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I don't see a session_start() in that second page's code pinehead18..
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

phenom, read closer, the function is IN the included file ;)
pinehead18
Forum Contributor
Posts: 329
Joined: Thu Jul 31, 2003 9:20 pm

Post by pinehead18 »

the session_start() happens in the include file since it is pulled on EVERY page of the site
Post Reply