[SOLVED] Another question on session start

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
bckennyw
Forum Newbie
Posts: 11
Joined: Wed Nov 26, 2003 10:25 am

[SOLVED] Another question on session start

Post 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.
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

anyway we can get the error?
bckennyw
Forum Newbie
Posts: 11
Joined: Wed Nov 26, 2003 10:25 am

Post 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.
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

can we get your code?
bckennyw
Forum Newbie
Posts: 11
Joined: Wed Nov 26, 2003 10:25 am

Post 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;

?>
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post 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.
bckennyw
Forum Newbie
Posts: 11
Joined: Wed Nov 26, 2003 10:25 am

Post by bckennyw »

Thanks a lot man! Problem fixed!
Post Reply