Jump to content

Recommended Posts

It's been a while since I've written any PHP, and I was writing a simple loggin... but I seam to have done something wrong...

 

 

<?php
error_reporting(0);
session_start(); 

// Connects to the database
require 'database.php';
$db = mysql_connect( "$location", "$dbuser", "$dbpass" );
mysql_select_db( "$database" );
if (!isset($_SESSION['log'])) {
  $bads = array("'","<",">",",","$",";",":",".");
  $user = $_POST['user'];
  $user = str_replace($bads,"",$user);
  $pass = $_POST['pass'];
  $pass = str_replace($bads,"",$pass);
  $pass = md5($pass);
  if( isset( $user ) & isset( $pass ) & ctype_alnum($user) ) {
   $sql = "select * from users where user = '$user' and pass = '$pass';";
   $rs = mysql_query($sql, $db);
   if( mysql_num_rows($rs) == 1 ) {
    $_SESSION['log'] = $user;
    break;
   }
   else {
    echo "Wrong username or password, please try again!";
    mysql_close();
   }
  }
  ?>
  <H2 ALIGN="CENTER">Roleplay Login</H2>
  <FORM ACTION="index.php" METHOD="POST">
   <TABLE WIDTH="200" BORDER="0" ALIGN="CENTER">
    <TR>
     <TD>Username</TD>
     <TD WIDTH="105"><INPUT NAME="user" TYPE="text" SIZE="15"></TD>
    </TR>
    <TR>
     <TD>Password</TD>
     <TD WIDTH="105"><INPUT NAME="pass" TYPE="PASSWORD" SIZE="15"></TD>
    </TR>
    <TR>
     <TD COLSPAN="2"><DIV ALIGN="CENTER"><INPUT TYPE="submit" VALUE="Login"></TD>
    </TR>
   </TABLE>
  </FORM>
  <?
}
if (isset($_SESSION['log'])) {

}
?>

 

It gets the info, and I do get the correct SQL sentance in $sql, but $rs seams to end up blank or not working... I'm sure its something simple I've overlooked.

Link to comment
https://forums.phpfreaks.com/topic/244040-my-logging-script-not-working/
Share on other sites

Try having just this

 

$rs = mysql_query($sql);

 

sometimes the second parameter has been an issue in my practice and it's not really that necessary I guess.

 

ALSO don't do that

 

$sql = "select * from users where user = '$user' and pass = '$pass';";

 

Instead have this

 

$sql = "select * from users where user = '$user';"

 

assuming your usernames are escaped and then fetch the array checking the password

Try below code

 

 

<?php
error_reporting(0);
session_start(); 

// Connects to the database
require 'database.php';
$db = mysql_connect( "$location", "$dbuser", "$dbpass" );
mysql_select_db( "$database" );
if (!isset($_SESSION['log'])) {
  $bads = array("'","<",">",",","$",";",":",".");
  $user = $_POST['user'];
  $user = str_replace($bads,"",$user);
  $pass = $_POST['pass'];
  $pass = str_replace($bads,"",$pass);
  $pass = md5($pass);
  if(isset($user) & isset($pass) & ctype_alnum($user)) {
  $sql = mysql_num_rows(mysql_query("SELECT * FROM `users` WHERE `user` = '$user' AND `pass` = '$pass'"));
  if($sql > "0") {
    $_SESSION['log'] = $user;
    break;
   }
   else {
    echo "Wrong username or password, please try again!";
    mysql_close();
   }
  }
  ?>
  <H2 ALIGN="CENTER">Roleplay Login</H2>
  <FORM ACTION="index.php" METHOD="POST">
   <TABLE WIDTH="200" BORDER="0" ALIGN="CENTER">
    <TR>
     <TD>Username</TD>
     <TD WIDTH="105"><INPUT NAME="user" TYPE="text" SIZE="15"></TD>
    </TR>
    <TR>
     <TD>Password</TD>
     <TD WIDTH="105"><INPUT NAME="pass" TYPE="PASSWORD" SIZE="15"></TD>
    </TR>
    <TR>
     <TD COLSPAN="2"><DIV ALIGN="CENTER"><INPUT TYPE="submit" VALUE="Login"></TD>
    </TR>
   </TABLE>
  </FORM>
  <?
}
if (isset($_SESSION['log'])) {

}
?>

Okay, thanks for all the help... as I said I had a feeling it was something really silly... and it was... I added:

 

if (!$db) {

    die('Could not connect: ' . mysql_error());

}

 

to my connection... and the problem was that I hadn't managed to get a connection to the database... mostly because the host used a diffrent internal address for the sql server... so localhost didn't work as I'm used to.

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.