Sessions ending after first page load

Discussion in 'PHP' started by shamess, Jun 30, 2006.

  1. #1
    I've not idea why my sessions aren't... sticking. They work for the first page load, and then they stop working and I get asked to log back in!

    This is my 'access.php' include file for protected pages. If you need any other information, lemme know.
    IF (session_id() == '') {
    	//if the login form has been submitted...
    	IF (isset ($sub_login)) {
    		$sql = "SELECT * FROM ".PREFIX."users WHERE `user_name` = \"$frm_name\" AND `password` = \"$frm_pass\"";
    		$query = mysql_query($sql);
    		IF (mysql_num_rows($query) == 1) {
    			//put them into a session
    			session_start();
    			$_SESSION['username'] = $_POST['frm_name'];
    			$_SESSION['password'] = $_POST['frm_password'];
    		//if the user name and password aren't correct
    		} else {
    			//start the page and show the form
    			$ext_title = "Incorrect Password";
    			include (PATH.'includes/header.php');
    			echo "<div id=\"content\">\n<b>Incorrect Password or Username</b>\n";
    			echo "<br />The username and/or password you entered are incorrect. Please enter your user name and password again.\n";
    			echo "<br /><form action=\"?\" method=\"POST\">\nUsername: <input type=\"text\" name=\"frm_name\" size=16>\n";
    			echo "<br />Password: <input type=\"text\" name=\"frm_pass\" size=20>\n";
    			echo "<br /><input type=\"submit\" value=\"Submit\" name=\"sub_login\"></form>\n";
    			echo "</div>\n";
    			include (PATH.'includes/footer.php');
    			echo "</body>\n</html>";
    			exit();
    		}
    	//if the login form hasn't been submitted
    	} else {
    		//start the page and show the form
    		$ext_title = "Log in";
    		include (PATH.'includes/header.php');
    		echo "<div id=\"content\">\n<b>Login In Form</b>\n";
    		echo "<br />Please enter your user name and password.\n";
    		echo "<br /><form action=\"?\" method=\"POST\">\nUsername: <input type=\"text\" name=\"frm_name\" size=16>\n";
    		echo "<br />Password: <input type=\"password\" name=\"frm_pass\" size=20>\n";
    		echo "<br /><input type=\"submit\" value=\"Submit\" name=\"sub_login\"></form>\n";
    		echo "</div>\n";
    		include (PATH.'includes/footer.php');
    		echo "</body>\n</html>";
    		exit();
    	}
    }
    PHP:
    If you'd like to see what's happening for yourself ... Databank; password is admin and user name is Administrator. Log in that, then click on the 'Databank' link in the nav bar and you're asked to log back in :/

    Thanks,
    Shamess
     
    shamess, Jun 30, 2006 IP
  2. kreoton

    kreoton Peon

    Messages:
    229
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    i think you must first start session, and then check session id ;)
     
    kreoton, Jul 1, 2006 IP
    shamess likes this.
  3. danielbruzual

    danielbruzual Active Member

    Messages:
    906
    Likes Received:
    57
    Best Answers:
    0
    Trophy Points:
    70
    #3
    session_start();

    has to be on top of the document for the sessions to work (i think)
     
    danielbruzual, Jul 1, 2006 IP
    shamess likes this.
  4. shamess

    shamess Well-Known Member

    Messages:
    1,127
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    185
    #4
    Yeah, they do >.< I comepletely forgot. Thanks!
     
    shamess, Jul 1, 2006 IP