Login Page $_SESSION

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
zatch
Forum Newbie
Posts: 18
Joined: Fri Jul 02, 2004 7:05 pm

Login Page $_SESSION

Post by zatch »

I just switched from using session_register() to $_SESSION and am having difficulties.

This is my login page script ... I think something is wrong with the setting sessions function. THANKS!

Code: Select all

<?php
include("includes/config.php");
$connection = mysql_connect($hostname, $user, $pass) or die ("Unable to connect to MySQL Database!");
$query = "SELECT * FROM accounts WHERE login = '$uname' AND password = PASSWORD('$upassword')";
$result = mysql_db_query($database, $query, $connection);


if (mysql_num_rows($result) == 1)
	{
	session_name("wdaccounts");
	session_start();
	$_SESSION['client_id'] = $clientid; 
	$_SESSION['client_name'] = $name;
	$_SESSION['client_login'] = $login;
		
	if ($uname == admin)
	{
	header("Location: admin.php");
	}
	else
	{
	header("Location: client.php");
	}
	mysql_free_result ($result);
	mysql_close($connection);
	}
else
	{
	echo "<center><font face=verdana>Login Error. Either you haven't logged in, or the Username/Password you entered were incorrect.</font></center><br><br>";
	mysql_free_result ($result);	
	mysql_close($connection);
	login_page(); 
		exit;
	}
function login_page() 
{ 

?>
<html> 
<title>WDAccounts Login</title>
<body> 
<form ACTION="<?=$_SERVER['PHP_SELF'];?>" method="POST"> <center>
<TABLE  align=center cellSpacing=1 cellPadding=1 width="40%" border=2 bordercolor=#929bab>
              <TR>
                <TD style="PADDING-LEFT: 10px; WIDTH: 33%; COLOR: #ffffff; BACKGROUND-COLOR:#929bab"><B>User name</B></TD>
                <TD><input type="text" name="uname" value="" size="20" maxlength="60"></TD>
              </TR>
              <TR>
                <TD style="PADDING-LEFT: 10px; WIDTH: 33%; COLOR: #ffffff; BACKGROUND-COLOR:#929bab"><B>Password</B></TD>
                <TD><input type="password" name="upassword" size="20"></TD></TR>
              </TABLE>
     <center><input name="submit" type="submit" value="Submit" /> </center>
            </form> 
</center>
</body> 
</html> 
<?php 
} 

?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

deleted
Last edited by John Cartwright on Sat Jul 10, 2004 1:04 am, edited 1 time in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Login Page $_SESSION

Post by John Cartwright »

Code: Select all

<?php 
include("includes/config.php"); 

function login_page() 
{ 

?> 
<html> 
<title>WDAccounts Login</title> 
<body> 
<form ACTION="<?=$_SERVER['PHP_SELF'];?>" method="POST"> <center> 
<TABLE  align=center cellSpacing=1 cellPadding=1 width="40%" border=2 bordercolor=#929bab> 
              <TR> 
                <TD style="PADDING-LEFT: 10px; WIDTH: 33%; COLOR: #ffffff; BACKGROUND-COLOR:#929bab"><B>User name</B></TD> 
                <TD><input type="text" name="uname" value="" size="20" maxlength="60"></TD> 
              </TR> 
              <TR> 
                <TD style="PADDING-LEFT: 10px; WIDTH: 33%; COLOR: #ffffff; BACKGROUND-COLOR:#929bab"><B>Password</B></TD> 
                <TD><input type="password" name="upassword" size="20"></TD></TR> 
              </TABLE> 
     <center><input name="submit" type="submit" value="Submit" /> </center> 
            </form> 
</center> 
</body> 
</html> 
<?php 
} 

if (!empty($_POST["submit"]))
{
$connection = mysql_connect($hostname, $user, $pass) or die ("Unable to connect to MySQL Database!"); 
$query = "SELECT * FROM accounts WHERE login = '".$_POST["uname"]."' AND password = '".$_POST["upassword"]."'" 
$result = mysql_db_query($database, $query, $connection); 

if (mysql_num_rows($result) == 1) 
   { 
   session_name("wdaccounts"); 
   session_start(); 
   $_SESSION['client_id'] = $_POST["clientid"]; 
   $_SESSION['client_name'] = $_POST["name"]; 
   $_SESSION['client_login'] = $_POST["login"]; 

       
   if ($uname == admin) 
   { 
   header("Location: admin.php"); 
   } 
   else 
   { 
   header("Location: client.php"); 
   } 
   mysql_free_result ($result); 
   mysql_close($connection); 
   } 
else 
   { 
   echo "<center><font face=verdana>Login Error. Either you haven't logged in, or the Username/Password you entered were incorrect.</font></center><br><br>"; 
   mysql_free_result ($result);    
   mysql_close($connection); 
   login_page(); 
   } 


}


?>
Post Reply