need help on user login

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

User avatar
zplits
Forum Contributor
Posts: 158
Joined: Sun Aug 03, 2008 8:59 pm

Re: need help on user login

Post by zplits »

Sorry for the late reply sir, I have just arrive home. :)
ok sir, here are the errors:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\wamp\www\online_DSS\checklogin.php on line 26

the above eror is from checklogin.php

Code: Select all

<?php
//ob_start();
$host = "localhost"; // Host name
$username = ""; // Mysql username
$password = ""; // Mysql password
$db_name = "minda"; // Database name
$tbl_name = "users"; // Table name
  
mysql_connect("$host", "root")or die("Connection Error: Cannot connect to Server");
mysql_select_db("$db_name")or die("MySQL Error: Cannot select Database");
 
// Define $myusername and $mypassword
$myusername=$_POST['username'];
$mypassword=$_POST['password'];
 
 
// To protect MySQL injection
//$myusername = stripslashes($myusername);
//$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
 
$sql="SELECT id FROM $tbl_name WHERE loginName='$myusername' and passWord='$mypassword'";
$result=mysql_query($sql);
 
    if (mysql_num_rows($result) > 0) {
          $data = mysql_fetch_assoc($result);
          $_SESSION['user'] = array('id' => $data['id'], 'firstName'=>$data['firstName']);
          header("location:loginSuccess.php");
          exit();
    } 
//ob_end_flush();
?>
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: need help on user login

Post by Stryks »

Okay ... insert this in line 25, just after under the mysql_query() call;

Code: Select all

 
if (!$result) {
    echo "Could not successfully run query ($sql) from DB: " . mysql_error();
    exit;
}
That should throw an error with any luck. Just post that error back and we should have a good idea of what is going on.
User avatar
zplits
Forum Contributor
Posts: 158
Joined: Sun Aug 03, 2008 8:59 pm

Re: need help on user login

Post by zplits »

Sorry for the very late reply sir,

I apologize....I cooked some food for my mom and dad.. Sorry to keep you waiting.
here is the error that your code displayed:

Could not successfully run query (SELECT id FROM users WHERE loginName='' and passWord='') from DB: Unknown column 'id' in 'field list'
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: need help on user login

Post by Stryks »

Well ... that's interesting.

Apparently you don't have a field named id in your table. You'll need to rectify that in your database.

Secondly, did you submit the form with blank username and password boxes? If you didnt, we need to find out why those values weren't inserted. Also, we'll need to check to make sure they aren't blank.
User avatar
zplits
Forum Contributor
Posts: 158
Joined: Sun Aug 03, 2008 8:59 pm

Re: need help on user login

Post by zplits »

Hello sir. I have figured out that i change a computer. I forgot to copy the mySQL file and paste it here. I'm sorry. To err is human. :D

Now i can access the login.php. But when i entered the admin then 1234. It doesn't load loginSuccess.php. It just refreshes the page.
User avatar
zplits
Forum Contributor
Posts: 158
Joined: Sun Aug 03, 2008 8:59 pm

Re: need help on user login

Post by zplits »

It process the checkLogin.php but it display this error sir,

Code: Select all

 
Parse error: syntax error, unexpected '{' in C:\wamp\www\online_DSS\loginSuccess.php on line 9
here is the code for the loginSuccess.php

Code: Select all

<?php
/*  session_start();
    if(!session_is_registered(myusername)){
    header("location:login.php");
    }
*/
    
    session_start();
  [b] if(!isset($_SESSION['user']){[/b]
   header("location:login.php");
    }
    
    
?>
the bolded code above is line 9 in my PC
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: need help on user login

Post by Stryks »

Sorry ... my bad.

Code: Select all

   session_start();
  if(!isset($_SESSION['user'])){
   header("location:login.php");
    }
Missed that close bracket. :roll:
User avatar
zplits
Forum Contributor
Posts: 158
Joined: Sun Aug 03, 2008 8:59 pm

Re: need help on user login

Post by zplits »

still no luck sir. It doesn't display any error anymore, but it still refreshes the page and won't let me access loginSuccess.php
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: need help on user login

Post by Stryks »

Ok ... just out of interest sake .... comment out the header() call at the start of loginsuccess.php.

You get forwarded now?
User avatar
zplits
Forum Contributor
Posts: 158
Joined: Sun Aug 03, 2008 8:59 pm

Re: need help on user login

Post by zplits »

yes sir. It forwarded me into loginSuccess.php. But it doesn't display my firstName. It just displays the buttons which says logout.
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: need help on user login

Post by Stryks »

Hah ... that's kind of funny. Well ... it is if what I think is the issue is in fact the issue.

First line of code on checklogin.php ...

Code: Select all

session_start();


It's always the simplest things that get overlooked.

You should also be able to un-comment that line from loginsuccess.php now.

Let me know how it goes (keep in mind that unless you have edited the page, it probably wont show the loginName just yet).

Cheers
User avatar
zplits
Forum Contributor
Posts: 158
Joined: Sun Aug 03, 2008 8:59 pm

Re: need help on user login

Post by zplits »

Hello sir, good morning. I just woke up. I'm from Philippines. How about you? where are you from?

Well, i followed what you said about session_start() at the first line of checklogin.php and it works. Only problem is it display the word "Array" and below it is the logout button.

Almost there sir. Thanks
User avatar
zplits
Forum Contributor
Posts: 158
Joined: Sun Aug 03, 2008 8:59 pm

Re: need help on user login

Post by zplits »

Here is my checklogin.php code:

Code: Select all

<?php
//ob_start();
session_start();
$host = "localhost"; // Host name
$username = ""; // Mysql username
$password = ""; // Mysql password
$db_name = "minda"; // Database name
$tbl_name = "users"; // Table name
  
mysql_connect("$host", "root")or die("Connection Error: Cannot connect to Server");
mysql_select_db("$db_name")or die("MySQL Error: Cannot select Database");
 
// Define $myusername and $mypassword
$myusername=$_POST['username'];
$mypassword=$_POST['password'];
 
 
// To protect MySQL injection
//$myusername = stripslashes($myusername);
//$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
 
$sql="SELECT id FROM $tbl_name WHERE loginName='$myusername' and passWord='$mypassword'";
$result=mysql_query($sql);
 
//code to check for errors
/*if (!$result) {
     echo "Could not successfully run query ($sql) from DB: " . mysql_error();
     exit;
}*/
 
$count=mysql_num_rows($result);
    if ($count==1) {
          $data = mysql_fetch_assoc($result);
          $_SESSION['user'] = array('id' => $data['id'], 'firstName'=>$data['firstName']);
          header("location:loginSuccess.php");
          exit();
    } 
    else {
         $error = "<b>ERROR</b>: Invalid Username / Password";
    }
 
//ob_end_flush();
?>
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: need help on user login

Post by Stryks »

Yeah ... I just crawled out of bed myself. We must be on similar time. I'm in Australia.

Looks like we're just about there.

Can I get you to post all of loginsuccess.php as it stands now.

Cheers.
User avatar
zplits
Forum Contributor
Posts: 158
Joined: Sun Aug 03, 2008 8:59 pm

Re: need help on user login

Post by zplits »

good morning. :) Oh you're from australia? nice country with lots of kangaroo. Never seen it in real life.

Here is the code for loginSuccess.php:

Code: Select all

<?php
/*  session_start();
    if(!session_is_registered(user)){
    header("location:login.php");
    }
*/
 
 
    session_start();
    if(!isset($_SESSION['user'])){
    header("location:login.php");
    }
 
    
?>
 
 
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="styles/login.css" rel="stylesheet" type="text/css"/>
<link rel="shortcut icon" href="images/favicon.ico">
<title>Welcome to Lourdes C</title>
</head>
 
<body>
<table width="974" border="0" cellpadding="0" cellspacing="0">
  <!--DWLayoutTable-->
  <tr>
    <td width="254" height="41" valign="top">
    <?php
        echo $_SESSION['user'];
    ?>                                   
    <form method = "POST" action = "logout.php">
    <input type = "submit" name="logout" value = "Logout">
    </form>
    </td>
    <td width="720">&nbsp;</td>
  </tr>
  <tr>
    <td height="959">&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>
</body>
</html>
Post Reply