简体   繁体   中英

creating login page with PHP

I am trying to create website with login form with some PHP code, were user will try to login with username and password and page will then show "welcome....". AT the moment when user try to put username and password website that shows up is blank, nothing is on it. Also i already have created mysql database with username and password.

this login form on my main page index.html:

<form id="form" method="post" action="login.php">
    <label for="username">Username:</label>
    <input type="text" name="username" size="15" required="required" />
    <label for="password">Password:</label>
    <input type="password" name="password" size="15" required="required" />
    <input id="loginButton" type="submit" name="submit" value="LOGIN" />
</form>

and this is my php page login.php:

<?php

session_start();

$host = "localhost";
$username = "*******";
$password = "*******";
$db_name = "********";
$tbl_name = "users";

$conn = mysql_connect("$host", "$username", "$password") or die("Cannot connect");
mysql_select_db("$db_name", $conn) or die("Cannot connect");

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

$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);

$count = mysql_num_rows($result);

if ($count == 1) {
    session_register("username");
    session_register("password");
    header("location:page1.html");
} else {
    echo "Wrong Username or Password";
}

?>

and on my welcome page - page1.html i have included some php code:

<?php

session_start();
if(!session_is_registered(username)){
    header("location:index.html");
}

?>

First off...dont store the password in the session. Thats just asking for trouble.

session_register("password");

Secondly....session_register() is a deprecated function and shouldn't be used anymore.

Instead do...

     $_SESSION['username'] = $myusername;

Third....

header("location:page1.html");

Should be a PHP file if you want sessions to work across pages..

header("location:page1.php");

Then in that PHP page do...

session_start();
if(!isset($_SESSION['username'])){
   header("location:index.php");
} else {
     // Display stuff to logged in user
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM