Jump to content

[SOLVED] Login and Sessions Form Help


CodeMama

Recommended Posts

Having problems getting my login form to work, Please help.

index.php

<?php include("inc/dbconn_open.php") ?>
<?php
$errs = error_reporting ('E_ALL');

if (isset($_POST['UserName'])) {$UserName = $_POST['UserName'];} else {$UserName = '';}
if (isset($_POST['Password'])) {$Password = $_POST['Password'];} else {$Password = '';}

$msg = '';

if (!empty($UserName)) {

    $sql = "SELECT * FROM admin WHERE UserName='$UserName' and Password='$Password'";
    $result = mysql_query ($sql);
    $row = mysql_fetch_object ($result);

    If (mysql_num_rows($result) > 0) {
        $_SESSION['AdminLogin'] = $row->AdminID;
        header ("Location: Main.php");
    } else {
        $msg = "Invalid Login";
    }
}

?>

<HTML>

<HEAD>
<TITLE>Work Order System - Administrative Section</TITLE>
<LINK REL="STYLESHEET" HREF="inc/style.css">
<script language="JavaScript">
<!--
    function leftTrim(sString) {
        while (sString.substring(0,1) == ' ') {
            sString = sString.substring(1, sString.length);
        }
        return sString;
    }
    
    function chkData1(objForm) {

        objForm.UserName.value = leftTrim(objForm.UserName.value);
        if (objForm.UserName.value.length == 0) {
            alert("Please enter your User Name.");
            objForm.Email.focus();
            return false;
        }
        
        objForm.Password.value = leftTrim(objForm.Password.value);
        if (objForm.Password.value.length == 0) {
            alert("Please enter a your Password.");
            objForm.Password.focus();
            objForm.Password.select();
            return false;
        }
        return true;
    }

//-->
</script>

</HEAD>

<BODY LEFTMARGIN="0" TOPMARGIN="0" MARGINWIDTH="0" MARGINHEIGHT="0">
<TABLE WIDTH="750" BORDER="0" CELLSPACING="0" CELLPADDING="0">

<TR>
<TD> </TD>
</TR>
<TR>
<TD ALIGN="CENTER"><B>Work Order System - Administrative Section</B><BR><BR></TD>
</TR>
<TR>
<TD>
<?php
    If (!empty($msg)){
        echo "<div class=\"cl_Error\">". $msg ."</div>";
    }
?>

<form name="form1" method="post" action="main.php?AdminID=<?php echo $AdminID; ?>" onSubmit="return chkData1(this)">
<TABLE WIDTH="300" BORDER="2" ALIGN="center" CELLPADDING="2" CELLSPACING="0" bordercolor="#000033" bgcolor="#0099CC">
    <TR>
        <TD HEIGHT="22"><div class="admin_Main">Username:</div></TD>
        <TD HEIGHT="22"> <INPUT TYPE="text" NAME="UserName"></TD>
    </TR>
    <TR>
        <TD><div class="admin_Main">Password:</div></TD>
        <TD><INPUT TYPE="password" NAME="Password"></TD>
    </TR>
    <TR>
        <TD colspan="2" align="center"><INPUT TYPE="submit" VALUE="Login"> </TD>
    </TR>
</TABLE>
</form>
</BODY>
</HTML>

Main.php
[code]<?php
include("inc/dbconn_open.php");
error_reporting ('E_ALL');

if (empty($_SESSION['AdminLogin']) OR $_SESSION['AdminLogin'] <> 'OK' ){
    header ("Location: LogOut.php");
}

if (isset($_SESSION['AdminID']) && !empty($_SESSION['AdminID'])){
    $AdminID = $_SESSION['AdminID'];
} else {
    header ("Location: LogOut.php");
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Work Order System - Administrative Section</title>
</head>

<frameset cols="200,*" frameborder="NO" border="0" framespacing="0">
  <frame src="Menu.php?AdminID=<?php echo $AdminID; ?>" name="leftFrame" scrolling="auto" noresize>
  <frame src="Welcome.php?AdminID=<?php echo $AdminID; ?>" name="mainFrame">
</frameset>
<noframes><body>
</body></noframes>
</html>

 

My session_start() is on the include db connection file....

Please need some clues...its not throwing errors on the page or in the error logs

Thanks in advance

 

[/code]

Link to comment
Share on other sites

Cleaned up:

 

<?php include("inc/dbconn_open.php") ?>
<?php
$errs = ini_set('error_reporting',E_ALL);

if (isset($_POST['UserName'])) {$UserName = $_POST['UserName'];} else {$UserName = '';}
if (isset($_POST['Password'])) {$Password = $_POST['Password'];} else {$Password = '';}

$msg = '';

if (!empty($UserName)) {

    $sql = "SELECT * FROM admin WHERE UserName='$UserName' and Password='$Password'";
    $result = mysql_query ($sql);
    $row = mysql_fetch_object ($result);

    If (mysql_num_rows($result) > 0) {
        $_SESSION['AdminLogin'] = $row->AdminID;
        header ("Location: Main.php");
    } else {
        $msg = "Invalid Login";
    }
}

?>

<HTML>

<HEAD>
<TITLE>Work Order System - Administrative Section</TITLE>
<LINK REL="STYLESHEET" HREF="inc/style.css">
<script language="JavaScript">
<!--
function leftTrim(sString){
while(sString.substring(0,1)==' '){
sString=sString.substring(1,sString.length)}
return sString}
function chkData1(objForm){
objForm.UserName.value=leftTrim(objForm.UserName.value)
if(objForm.UserName.value.length==0){
alert("Please enter your User Name.")
objForm.Email.focus()
return false}
objForm.Password.value=leftTrim(objForm.Password.value)
if(objForm.Password.value.length==0){
alert("Please enter a your Password.")
objForm.Password.focus()
objForm.Password.select()
return false}
return true}

//-->
</script>

</HEAD>

<BODY LEFTMARGIN="0" TOPMARGIN="0" MARGINWIDTH="0" MARGINHEIGHT="0">
<TABLE WIDTH="750" BORDER="0" CELLSPACING="0" CELLPADDING="0">

<TR>
<TD> </TD>
</TR>
<TR>
<TD ALIGN="CENTER"><B>Work Order System - Administrative Section</B><BR><BR></TD>
</TR>
<TR>
<TD>
<?php
    If (!empty($msg)){
        echo "<div class=\"cl_Error\">". $msg ."</div>";
    }
?>

<form name="form1" method="post" action="main.php?AdminID=<?php echo $AdminID; ?>" onSubmit="return chkData1(this)">
<TABLE WIDTH="300" BORDER="2" ALIGN="center" CELLPADDING="2" CELLSPACING="0" bordercolor="#000033" bgcolor="#0099CC">
    <TR>
        <TD HEIGHT="22"><div class="admin_Main">Username:</div></TD>
        <TD HEIGHT="22"> <INPUT TYPE="text" NAME="UserName"></TD>
    </TR>
    <TR>
        <TD><div class="admin_Main">Password:</div></TD>
        <TD><INPUT TYPE="password" NAME="Password"></TD>
    </TR>
    <TR>
        <TD colspan="2" align="center"><INPUT TYPE="submit" VALUE="Login"> </TD>
    </TR>
</TABLE>
</form>
</BODY>
</HTML>

Main.php
[code]<?php
include("inc/dbconn_open.php");


if (empty($_SESSION['AdminLogin']) || $_SESSION['AdminLogin'] <> 'OK' ){
    header ("Location: LogOut.php");
exit;
}

if (isset($_SESSION['AdminID']) && !empty($_SESSION['AdminID'])){
    $AdminID = $_SESSION['AdminID'];
} else {
    header ("Location: LogOut.php");
    exit;
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Work Order System - Administrative Section</title>
</head>

<frameset cols="200,*" frameborder="NO" border="0" framespacing="0">
  <frame src="Menu.php?AdminID=<?php echo $AdminID; ?>" name="leftFrame" scrolling="auto" noresize>
  <frame src="Welcome.php?AdminID=<?php echo $AdminID; ?>" name="mainFrame">
</frameset>
<noframes><body>
</body></noframes>
</html>

Link to comment
Share on other sites

  • 2 weeks later...

Hi guys, Ok I am still pulling my hair out on this, I did have it working but setting the $AdminID = "ok" and then it just logged in and passed OK in the url..but that will not work since certain users have certain admin fuctions pulled from the db according to that AdminID, so when it got to those pages it was just looking at the db for adminID with OK to load the functions....I have tried to store as a variable, I'm really stuck and just keep making it worse I think... Here are the two scripts, someone please show me how I take the AdminID from page to page throughout this application , I am not declaring or "setting" it right....I'm so tired!

 

Index.php


<?php 
session_start(); //start session

include("inc/dbconn_open.php") ; //connect to the db

if (isset($_POST['UserName'])) {$UserName = $_POST['UserName'];} else {$UserName = '';}//user
if (isset($_POST['Password'])) {$Password = $_POST['Password'];} else {$Password = '';}//password

$msg = ' Please enter your Username and Password';

if (!empty($UserName)) {

    $sql = "SELECT * FROM admin WHERE UserName='$UserName' and Password='$Password'";
    $result = mysql_query ($sql);
    $row = mysql_fetch_object ($result);

         If (mysql_num_rows($result) > 0) {
         $_SESSION['AdminLogin'] = $row->AdminID;
         header ("Location: Main.php");
   } else {
        $msg = "Invalid Login";
    }

}

?>

 

which is supposed to pass to main.php

<?php
session_start();
include("inc/dbconn_open.php");

if (empty($_SESSION['AdminLogin']) OR $_SESSION['AdminLogin'] <> 'YES' ){
    header ("Location: LogOut.php");
}

//if (isset($_GET['AdminID']) && !empty($_GET['AdminID'])){
    
//}
// else {
    //header ("Location: LogOut.php");
//}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Work Order System - Administrative Section</title>
</head>

<frameset cols="200,*" frameborder="NO" border="0" framespacing="0">
  <frame src="Menu.php?AdminID=<?php echo $_SESSION['AdminLogin']; ?>" name="leftFrame" scrolling="auto" noresize>
  <frame src="Welcome.php?AdminID=<?php echo $_SESSION['AdminLogin']; ?>" name="mainFrame">
</frameset>
<noframes><body>
</body></noframes>
</html>

 

 

Link to comment
Share on other sites

Thanks for looking,

AdminID is an ID stored in a db, the users are given one when their acct is set up, and it stores what they can and cannot do in the system.

I made a couple changes, just re-read the O'reilly chapter on Sessions but I still stuck ... blah...

so now in index.php I am trying to registers the login, so I can SUPPOSEDLY use it later that is where i"m stuck its not getting used to open the next page...

index.php

<?php 
session_start(); //start session
session_register('AdminID');
session_register('UserName');
session_register('Password');

include("inc/dbconn_open.php") ; //connect to the db

if (isset($_POST['UserName'])) {$UserName = $_POST['UserName'];} else {$UserName = '';}//user
if (isset($_POST['Password'])) {$Password = $_POST['Password'];} else {$Password = '';}//password

$msg = ' Please enter your Username and Password';

if (!empty($UserName)) {

    $sql = "SELECT * FROM admin WHERE UserName='$UserName' and Password='$Password'";
    $result = mysql_query ($sql);
$row = mysql_fetch_object ($result);


     If (mysql_num_rows($result) > 0) {
	 $_SESSION['AdminLogin'] = $row->$AdminID;
         header ("Location: Main.php");
   } else {
        $msg = "Invalid Login";
    }

}

?>

 

 

Link to comment
Share on other sites

<?php
class admin{
function register_admin()

{
$sql = "SELECT * FROM admin WHERE UserName='$UserName' and Password='$Password'";
    $result = mysql_query ($sql);
   $row = mysql_fetch_object ($result);
  If (mysql_num_rows($result) > 0) {
     $result=  $_SESSION['AdminLogin'] = $row->$AdminID;}

return $result;
}
}

$call_result= new admin;

echo $call_result->register_admin();
?>

Link to comment
Share on other sites

Thanks for looking guys, ok I do have my error reporting on I just put the error_reporting(E_All); back in my code from above.

I have made progress in that, now when I login it does go to the main.php yet does not load the menus to go with the AdminID its just a blank page.

the code now is :

index.php

<?php 
error_reporting(E_ALL);

session_start(); //start session
session_register('AdminID');
session_register('UserName');
session_register('Password');

include("inc/dbconn_open.php") ; //connect to the db

if (isset($_POST['UserName'])) {$UserName = $_POST['UserName'];} else {$UserName = '';}//user
if (isset($_POST['Password'])) {$Password = $_POST['Password'];} else {$Password = '';}//password

$msg = ' Please enter your Username and Password';

if (!empty($UserName)) {

    $sql = "SELECT * FROM admin WHERE UserName='$UserName' and Password='$Password'";
    $result = mysql_query ($sql);
   $row = mysql_fetch_object ($result);
   

        If (mysql_num_rows($result) > 0) {
       $_SESSION['AdminID'] = $row->$AdminID;
         header ("Location: Main.php");
   } else {
        $msg = "Invalid Login";
    }

}

?>

 

and the main.php

<?php
session_start();
include("inc/dbconn_open.php");

if (empty($_SESSION['AdminID']) OR $_SESSION['AdminID'] <> 'YES' ){
   header ("Location: LogOut.php");
}

if (isset($_GET['AdminID']) && !empty($_GET['AdminID'])){
   
}
else {
   header ("Location: LogOut.php");
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Work Order System - Administrative Section</title>
</head>

<frameset cols="200,*" frameborder="NO" border="0" framespacing="0">
  <frame src="Menu.php?AdminID=<?php echo $_SESSION['AdminID']; ?>" name="leftFrame" scrolling="auto" noresize>
  <frame src="Welcome.php?AdminID=<?php echo $_SESSION['AdminID']; ?>" name="mainFrame">
</frameset>

 

And you will see above how it was passing in the URL and that is what I DON"T WANT but can't figure out how to make it work

Link to comment
Share on other sites

it's going to be my next step , I figured if I can't get what should be a simple session to go from page to page, surely OOP classes are going to confuse me ever worse. :( but here I go... its got to be something with the way i'm trying to use the session $adminID on the other pages. thanks freak for trying to help me none the less!

Link to comment
Share on other sites

its really not that hard to get its just a class that gets the admin if its greater than 0. and returns it so all you have to do is call

<?php
echo $call_result->register_admin();?>

instead of

<?php
echo $_SESSION['Adminid'];?>

where your passing it thru the url

 

 

Link to comment
Share on other sites

did you try adding session_start() to the class  ???

 

<?php
class admin{
function register_admin()

{
session_start(); //start session
session_register('AdminID');
session_register('UserName');
session_register('Password');
$sql = "SELECT * FROM admin WHERE UserName='$UserName' and Password='$Password'";
    $result = mysql_query ($sql);
   $row = mysql_fetch_object ($result);
  If (mysql_num_rows($result) > 0) {
     $result=  $_SESSION['AdminLogin'] = $row->$AdminID;}

return $result;
}
}

$call_result= new admin;

echo $call_result->register_admin();
?>

Link to comment
Share on other sites

Oh and Currently the code is this, and this would work if I could define AdminID as the to use later if I echo AdminID is just saying its OK

 

So when I try to pull something from the db according to the AdminID I of course get nothing because there are none in the db that are OK they are actual ID's....

 

My crappy code

<?php 
session_start();
error_reporting(E_ALL);
include("inc/dbconn_open.php") ;

if (isset($_POST['UserName'])) {$UserName = $_POST['UserName'];} else {$UserName = '';}
if (isset($_POST['Password'])) {$Password = $_POST['Password'];} else {$Password = '';}

$msg = '';

if (!empty($UserName)) {

    $sql = "SELECT * FROM admin WHERE UserName='$UserName' and Password='$Password'";
    $result = mysql_query ($sql);
$row = mysql_fetch_object ($result);

If (mysql_num_rows($result) > 0) {
	$_SESSION['AdminLogin'] = 'OK';
	header ("Location: Main.php?AdminID=". $_SESSION['AdminLogin']);
} else {
	$msg = "Sorry You Entered  An Invalid Login";
}
}

?>

Passes to Main.php

<?php
session_start();
error_reporting(E_ALL);
include("inc/dbconn_open.php");

if (empty($_SESSION['AdminLogin']) OR $_SESSION['AdminLogin'] <> 'OK' ){
header ("Location: LogOut.php");
}

if (isset($_GET['AdminID']) && !empty($_GET['AdminID'])){
$AdminID = $_GET['AdminID'];
} else {
header ("Location: LogOut.php");
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Work Order System - Administrative Section</title>
</head>

<frameset cols="200,*" frameborder="NO" border="0" framespacing="0">
  <frame src="Menu.php?AdminID=<?php echo $_SESSION['AdminLogin']; ?>" name="leftFrame" scrolling="auto" noresize>
  <frame src="Welcome.php?AdminID=<?php echo $_SESSION['AdminLogin']; ?>" name="mainFrame">
</frameset>
<noframes><body>
</body></noframes>
</html>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.