View Full Version : PHP and MySQL Log In Sessions

06-29-2007, 11:44 PM
Hi. I'm trying to learn PHP, and I have a good idea on what to do on some things, but this one I have absolutely no idea. So, please go easy on me.

Anyways, I'm using a MySQL table called 'manager_users' with three columns. One column is the user name column (user_name), the second is the password column (password), and the third is the e-mail address column (e-mail_address). My code is set up like this, the form is on a page (?page=) and my action for the form on the page is like this (?action=log_in). So, I think I have the log in down, but I'm not sure. I need a way to make sure it's actually logging in, and I need a way to make sure it's logging out. Also, I don't know how I would, but I need it to check the forms so that when a form is left blank, it notifies the user.

Here's the code:

mysql_connect('mysql.medicatedsquid.com', 'medicatedsquid', '18e1ns');
switch($_GET['page']) {

/* Home Page/Log In */
case '';
if($_SESSION ['logged_in'] = 'false') {
echo 'welcome. <a href="?action=log_out">log out</a>';
} else {
echo '<form method="post" action="?action=log_in">
<table align="center" border="0" cellspacing="10" cellpadding="0">
<td>User Name:</td>
<td><input type="text" name="user_name" /></td>
<td><input type="text" name="password" />
<td><input type="submit" name="log_in" value="Log In" /></td>

switch($_GET['action']) {

/* Log In Action */
case 'log_in';
$user_name = $_POST['user_name'];
$user_name = $_SESSION['user_name'];
$password = $_POST['password'];
$query1 = mysql_query("SELECT * FROM manager_users WHERE user_name = '$user_name' and password = '$password'");
$num_rows1 = mysql_num_rows($query1);
if($num_rows > 0) {
$_SESSION['logged_in'] = 'true';
} else {
$_SESSION['logged_in'] = 'false';

/* Log Out Action */
case 'log_out';
$_SESSION['logged_in'] = 'false';


(Yes, I know, I didn't tab my code.) Thanks in advance!

06-30-2007, 01:00 AM
If I understand what you're asking for, you want to know how to determine a user is logged in? To do that, you should add a field in the table like 'logged_in' and set it to true or false or 0 or 1.

06-30-2007, 01:58 AM
I would recommend using the statement:

or die(mysql_error())
You could insert this after the switch and ifs of your code.

As for checking whether the form is filled or not, you should use javascript for that. Add onsubmit to your input tag

<input type="submit" name="log_in" value="Log In" onsubmit="validate()" />

add this script tag on the header part of your site

<script type="text/javascript">
function validate() {
var nameBox = document.forms[0].elements[0]
var pswdBox = document.forms[0].elements[1]
if (nameBox.value == "" || pswdBox.value == "") {
alert("Please fill in all fields!")
return false
else {
return true

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum