J.Hof J.Hof - 1 year ago 53
HTML Question

my login page opening first

I have a login.php page which I want the user to have to use when they click on Add or Delete buttons on the html form I have (also they must not remain logged in, if they leave the page and click Add or Delete then they must login again).

Currently I have it set to take the user to index.php if they login correctly but I would like to remove that once I figure out how to make them login to access certain pages.

my login.php code:

<html>
<head>
<title>Login</title> <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body id="body-color">
<div id="Sign-In">
<center><fieldset style="width:30%"><legend>Welcome Please Login Below</legend>
<form method="POST" action="connectivity.php">
Username: <br><input type="text" name="user" size="40"><br>
Password: <br><input type="password" name="pass" size="40">
<br>
<br>
<input id="button" type="submit" name="submit" value="Log-In">
</form>
</center>
</fieldset>
</div>
</body>
</html>


connectivity.php:

<?php

define('DB_HOST', 'localhost');
define('DB_NAME', 'list');
define('DB_USER', 'root');
define('DB_PASSWORD', '****');
$con = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error());
$db = mysql_select_db(DB_NAME, $con) or die("Failed to connect to MySQL: " . mysql_error());
/*
$ID = $_POST['user']; $Password = $_POST['pass'];
*/

function SignIn() {
session_start();
if (!empty($_POST['user'])) {
$query = mysql_query("SELECT * FROM UserName where userName = '$_POST[user]' AND pass = '$_POST[pass]'") or die(mysql_error());
$row = mysql_fetch_array($query);
if (!empty($row['userName']) AND ! empty($row['pass'])) {
$_SESSION['userName'] = $row['pass'];
header("Location: index.php");
} else {
header("Location: login.php");
}
}
}

if (isset($_POST['submit'])) {
SignIn();
}
?>

Answer Source

Use SESSIONS. Set a Session with a value (1) and if its not set, it would bring you to your login page.

Here is an example:

session_start();

if (!isset($_SESSION['CheckLogin'])) {
    header("Location: index.php");
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download