Recently I tried to create a login webpage for my project. So there's the index.html, the login page where the person has to login. It sends the query to checklogin.php which is this:
<?php
ob_start();
$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("wordshare.zxq.net", "754319_guest", "guest")or die("cannot connect");
mysql_select_db("wordshare_zxq_users")or die("cannot select DB");
// Define $myusername and $mypassword
$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 members 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_start();
session_register("myusername");
session_register("mypassword");
header("location:main.php");
}
else {
header("location:index.html");
}
ob_end_flush();
?>
Then the main page checks for the session, and if you are not logged in it redirects you to index.html and the code is as following:
<?php
// Connects to your Database
mysql_connect("wordshare.zxq.net", "754319_guest", "guest")or die("cannot connect");
mysql_select_db("wordshare_zxq_users")or die("cannot select DB");
//checks cookies to make sure they are logged in
session_start();
session_start();
if(!session_is_registered(myusername)){
header("location:index.html");
}
?>
The problem is that I don't get redirected back to the main page if I go directly to the main.html. I've tried using echo to find out whether the session is recorded, and it returned undefined. What really baffled me was that even when I try to print out something else like a word, it also returns me "undefined". Can anyone help me?