Page 1 of 1

[SOLVED] Another question on session start

Posted: Wed Nov 26, 2003 3:25 pm
by bckennyw
For an unknown reason, my PHP won't work when I "attach" it with any session-things. (It works perfectly when I take out all the session-related codes.)

I did put the session_start() at the first line of the document. But it still gives me the same error.

Is there anything I need to enable on the server before using those session-related functions?

Thanks for your help in advance!

p.s. I did NOT use any cookies with the session.

Posted: Wed Nov 26, 2003 3:30 pm
by infolock
anyway we can get the error?

Posted: Wed Nov 26, 2003 3:33 pm
by bckennyw
Hm... there isn't actually an error message printed out. It just gives me a blank page (Yes, a blank page.) whenever I use anything that deals with session.

Posted: Wed Nov 26, 2003 3:35 pm
by d3ad1ysp0rk
can we get your code?

Posted: Wed Nov 26, 2003 3:38 pm
by bckennyw
Ooops!!! :-) Forgot that one.

It just gives me a blank page when I click "Submit" in the first page.

First page (login.php):

Code: Select all

<?php echo "<?xml version="1.0" encoding="iso-8859-1"?".">"; ?>
<!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>Login Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>

<body>
<form name="loginForm" id="loginForm" method="post" action="sfLoginProcess.php">
  <p align="center">Sutton Farms Login Page</p>
  <p align="center">Username: 
    <input name="username" type="text" id="username" />
    <br />
    Password: 
    <input name="password" type="password" id="password" />
  </p>
  <p align="center"> 
    <input type="submit" name="Submit" value="Submit" />
    &nbsp; 
    <input type="reset" name="Clear" value="Reset" />
  </p>
</form>
<p>&nbsp;</p>
</body>
</html>

The processing page:

Code: Select all

<?php 

// Session begins
// Avoid weird error occurs
session_start();

// Get variables from previous page
import_request_variables("p", "login_");

// Connection to database
// Database variables
$serverAddress = "localhost";
$user = "username";
$pwd = "password";
$database = "db";
$table = "table";
// Establish connection
mysql_connect($serverAddress, $user, $pwd) or die("Cannot connect to MySQL database.");

// Select target database
mysql_select_db($database) or die("Cannot select target database.");

// Verifying username and password
// Get username & password from previous form
$username = $login_username;
$password = $login_password;
// Setup query
$query = "SELECT password, type FROM " . $table . " WHERE USERNAME = '". $username . "'";
// Query the database
$result = mysql_query($query) or die("Cannot submit query.");
// Check if the passwords match
if ($password != mysql_result($result, 0, 'password')) &#123;
	// Does not match.
	// Close the database connection.
	mysql_close();
	// Redirect to fail page.
	header("Location: fail.html");
&#125; else &#123;
	// Passwords match.
	// Close the database connection.
	mysql_close();
	
	/*
	// Storing variables in sessions
	// Register variables with the session
	session_register('username') = $login_username;
	session_register('password') = $login_password;
	session_register('type') = $mysql_result($result, 0, 'type');
	*/
	/*
	// Make sure the session variables have been written
	session_write_close();
	*/
	// If the user is an admin
	if (mysql_result($result, 0, 'type') == "admin") &#123;
		// Direct to the admin page
		header("Location: admin.php");	
	&#125; else &#123;
		// Direct to the user page
		header("Location: user.php");
	&#125;
&#125;

?>

Posted: Wed Nov 26, 2003 3:44 pm
by microthick
This is probably not the cause of your problem, but just throwing an idea out.

Using session_register() requires register_globals to be on. If they are off, you have to use the $_SESSION[] array.

Posted: Wed Nov 26, 2003 3:55 pm
by bckennyw
Thanks a lot man! Problem fixed!