Hi,
I'm using a pretty simple login script for a part of a website I have. It worked great on my server which was running php version 4 something. Sorry I don't know the exact one.
But when I switched to my new server which is running php 5 the login script no longer works, the problem seems to be the start_session() function. I've checked everything else and it seems to be good.
Anyways, what the problem is, is that when you login with the wrong password it sends you back with the error message which is good, but when you login using the correct info it just sends you back to the login form without sending you to the specfied page like it's supposed to. Does anyone have any idea why it would do this?
It's really confusing me.
Here's my code:
checklogin.php
<?php
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="members"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
// To protect MySQL injection (more detail about MySQL injection)
$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='$mypassword'";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>
loginsuccess.php (where you go when it's successful, of course)
<?
session_start();
if(!session_is_registered(myusername)){
header("location:main_login.php");
}
?>
<html>
<body>
Login Successful
</body>
</html>
It seems as though everytime you get to loginsuccess.php it says the session is not registered and sends you back to the login form. I'm so confused as to why it's doing this seeing as it was working great on my old server.
Any help would be greatly appreciated!!