I am having problem on this code of mine, I made a short javascript pop-up notice everytime a user logs a incorrect password or a correct pass, and when I typed the correct username and password, it is still invalid but when i delete these codes:

session_register('myusername');
session_register('mypassword');
$_SESSION['myusername']==$myusername;

it logs in well and there's no little security on my login... Please help me guys!

PHP Code:

<?php
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password=""; // Mysql password 
$db_name="db"; // Database name 
$tbl_name="members"; // Table name

mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");


$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword'];


$encrypted_mypassword=md5($mypassword);


$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$encrypted_mypassword'";
$result=mysql_query($sql);

$count=mysql_num_rows($result);

if($count == '1') {

session_register('myusername');
session_register('mypassword');
$_SESSION['myusername']==$myusername;
echo '1';
}
else 
{
  echo '0';
}
?>

Javascript Login:

<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$(".loading").hide();
$(".message").hide();
$("#login_form").submit(function()
{
$(".login_form").hide();
$(".loading").fadeIn(100);
$.post("checklogin.php",{ myusername:$('#myusername').val(),mypassword:$('#mypassword').val()} ,function(data)
{
$(".loading").hide();
if(data == '1')
{
$('.message').html('<p>Success - Redirecting...</p>');
window.location.replace("members/ld.php");
}
else
{
$('.message').html('<p>Login Failed - Please Try Again</p>');
$(".login_form").fadeIn("slow");
}
$(".message").fadeIn("slow").delay(700).fadeOut(700);
});
return false;
});
});
</script>

Login Form:

<center><div class="message" style="color:red;font-weight:bold"></div></center>
			<form id="login_form" name="form1" method="post" action="" class="login">
				<h2>Login</h2><br class="spacer" />
				<label>Username: </label><br class="spacer" />
				<input name="myusername" type="text" id="myusername" /><br class="spacer" />
				<label>Password: </label><br class="spacer" />
				<input name="mypassword" type="password" id="mypassword" /><br class="spacer" />
				<input name="" type="checkbox" value="" class="check" /><p>Remember my password</p>
				<input name="button" id="button" value="Submit" type="image" src="images/login_btn.jpg" title="Login" class="loginBtn" />
				<!-- <a href="#" class="registerBtn"><img src="images/register_btn.jpg" title="Register"/></a> -->
			</form><br class="spacer" />

To solve this problem simply delete the session_register() function wherever you have used it and also at the top of each page (on line 1) place the following.

<?php session_start();

That should solve it.

It doesn't work, I put the code that you gave me in the first line below the <?php and also deleted the session_register, and still no luck, any other solutions? Thanks by the way.

Do you have cookies enabled because having cookies disabled will disable sessions. If you want to know how to use sessions with cookies disabled then click here.

Holy Epic! Thanks man now it works! SOLVED!

use this $_SESSION=$myusername; in place of

$_SESSION==$myusername;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.