Jump to content

Keeping A Session


stormx

Recommended Posts

Well since my server migration I've been having a heap of trouble with this script of mine; I know the issue is with my script and not the server. The problem is that this specific script is not keeping a session.

 

Here is what starts the session, it works because I get the logged in message:

 

<?php
// lets start at the removal of sh1t.
    $username = mysql_real_escape_string($_POST['username']);
    $password = mysql_real_escape_string(md5($_POST['password']));

    // Check the database...
    $login_sql = mysql_query("SELECT `id`, `username`, `password` FROM `users` WHERE `username` = '$username' AND `password` = '$password'");
    $users = mysql_num_rows($login_sql);
    $users_array = mysql_fetch_array($login_sql);
    
    // Lets check if the user is found.
    if($users == 1) {

      $_SESSION['thegame_user'] = $users_array['username'];
      $_SESSION['thegame_id'] = $users_array['id'];
      session_start();
      // Logged In..
      echo 'Welcome to <strong>The Game</strong>, '.$username.'! Please push "Play" to continue.';

} else {
      // Error..
      echo 'You have entered a wrong username or password in. Please try again by pressing the back button.';
    }
?>

I included session_start(); in that one.. before the // Logged In.. comment

 

Now this is where I'm experiencing issues:

 

<?php

// OK, Lets check if the user is logged in.
if(!isset($_SESSION['thegame_user'])) {
  // Error time!
  echo '<h2>Error!</h2>';
  echo '<center class=special>Not Logged in at the moment. Click login on the side.</center>';
} else {
// Ok they are... continued.
echo 'Welcome';
}
?>

 

I keep getting the Not Logged in error.

 

Last of all, here is my config.php file:

 

<?php
mysql_connect("localhost", "xxx", "xxxx") or die("Could not connected to \"The Game\". Please email: [email protected]");
mysql_select_db("xxx") or die("Cannot select the database. Please email: [email protected]");

$global_user = $_SESSION['thegame_user'];
$user_sql = mysql_query("SELECT * FROM `users` WHERE `username` = '$global_user'");
$user = mysql_fetch_array($user_sql);

?>

 

Sorry it wasn't simple, but this issue I cannot seem to find a fix, hope you guys can help.. much appreciated.

Link to comment
https://forums.phpfreaks.com/topic/138825-keeping-a-session/
Share on other sites

In the first script you showed us, you are calling session_start() in the "middle" of the page.  It works, but not because the session was enabled, but because the mysql_query was successful.

 

You should try moving the session_start() line to the top of the page on that script.

Otherwise, this sounds like a problem I was having doing a similar thing... my solution was to explicitly set the session cookie when I was calling the session_start() midway down the page.  This was because I didn't want to give away a(n authenticated) session to someone who had not authenticated.

Link to comment
https://forums.phpfreaks.com/topic/138825-keeping-a-session/#findComment-725915
Share on other sites

I moved the session_start() onto line 2 of the login page but know I'm getting the following syntax error:

 

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/xxx/public_html/xxx/xxx/index.php:14) in /home/xxx/public_html/xxx/xxx/login.php on line 2

Link to comment
https://forums.phpfreaks.com/topic/138825-keeping-a-session/#findComment-725923
Share on other sites

err, is there more PHP/HTML above that script?  I assumed that script was the entire page.. whatever that page that that script is on, move session_start() to the top of the page..  Make sure you don't have any HTML output of whitespace before the top of the page..

 

<?php
session_start();

//more code possibly
?>
anything else

Link to comment
https://forums.phpfreaks.com/topic/138825-keeping-a-session/#findComment-725929
Share on other sites

My code now looks like this - index.php:

 

<?php 
session_start();
include "config.php";
?>
<!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>
<title>Class Of - The Game</title>
<link type="text/css" rel="stylesheet" href="css/stylechrissy.css" />
</head>
<body>
        <?php
	if($_GET['act']) {
        $file = $_GET['act'].".php";
        if(file_exists($file)) {
        include($file);
        } else {
        echo '<h2>No file found</h2>
	<p>The file you have defined is not found.</p>';
        }
	} else {
	?>
	    Make sure you are registered at The Game so you can recieve the full experience of the site.
 	<?php
	}
	?>      
</body>
</html>

 

I also am able to echo the session ID on the login page.. so that seems to be working just can't continue the session onto other pages.

Link to comment
https://forums.phpfreaks.com/topic/138825-keeping-a-session/#findComment-725935
Share on other sites

Archived

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

×
×
  • Create New...

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.