major php issues
I am trying to create a simple login system. When I run the login form (with the correct username and password) it doesn't seem to run the php. Any suggestions?
<?php
$host="linuxserver"; // Host name
$username="jparry2"; // Mysql username
$password=""; // Mysql password
$db_name="jparry2"; // Database name
$tbl_name="customer"; // Table name
// Connect to server and select databse.
mysqli_connect("$host", "$username", "$password")or die("cannot connect");
mysqli_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
$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=mysqli_query($sql);
// Mysql_num_row is counting table row
$count=mysqli_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";
}
?>
<html>
<body>
</body>
</html>
edit added login form code
<html>
<head><title>Login</title></head>
<body>
<form action='checklogin.php'
method='POST' style='margin: .5in'>
&开发者_开发问答lt;p><label for='user_name' style='font-weight: bold;
padding-bottom: 1em'>USER ID: </label>
<input type='text' name='myusername' id='myusername'
value='' /></p>
<p><label for='password' style= 'font-weight: bold'>Password: </label>
<input type='password' name='mypassword' id='mypassword'
value='' /></p>
<p><input type='submit' value='Login'> </p>
<input type='hidden' name='sent' value='yes'/>
<a href= "/home/jparry2/public_html/register.php">Register</a>
</form>
</body>
</html>
If your browser asks you to download the php file it means the php interpreter is not being invoked. i.e. you don't have it installed or configured correctly.
Are you getting any error message? Seems ok to me. Have you tried echoing something in the if-block for example? That might help you understand what's wrong.
Some things you could check or try:
- Have you got error reporting on?
- Put `var_dump($_POST); die(); on the top of the page to see if the $_POST variables are submitted correctly.
- Make sure you are not outputting anything to the browser before the
header()
function. If you have error_reporting off and you outputted something to the browser, usingheader()
will result in a fatal error which could cause a blank white page.
A few other notes from your code:
- You don't need to put variables inside double quotes, they work on their own:
mysqli_select_db("$db_name")
becomesmysqli_select_db($db_name)
- You don't need to
stripslashes()
if you're doingmysql_real_escape_string
. The latter will handle the job on its own.
In some browsers, the Location
header is case-sensitive, and thus your header("location:login_success.php");
call might not be working (a comment on the header
documentation page suggests that this occurs in IE7). Try capitalizing the l
in Location.
You don't do any "session_start()", so your session can't be used.
Maybe you need it to started in your "login_success.php" script.
I agree with Daniel, by revising header("Location: login_success.php");
Also, as a side note since at the time of writing this, it wasn't clearly explained what didn't work, but you when adding session variables you need to have session_start()
.
Also try to use $_SESSION['variable']
since session_register()
is deprecated as of PHP 5.30 taken from PHP: session_register try something like this
if($count==1){
session_start();
// Register $myusername, $mypassword and redirect to file “login_success.php”
$_SESSION['username'] = $myusername;
$_SESSION['mypassword'] = $mypassword;
session_write_close(); // makes sure nothing was lost during redirect
header('Location: nextpage.php');
}
精彩评论