Hello
This is a simple php login script with sessions. It connects to mySQL for usernames and passwords.
I’m using a variation of the code from http://www.phpportalen.net/wiki/index.php?page=Enkel+Inloggning+med+Mysql+och+sessioner+-+Komplett+kod (index.php is the relevant part)
Here is my code (my script is called login.php):
<?php
session_start(); // Always at the top
include "conn.php"; // Databaseconnection
include "functions.php"; // Functions
// Login
if (isset($_POST['submit'])){
$_POST = db_escape($_POST);
$passwd = safepass($_POST['passwd']);
$sql = "SELECT id FROM members
WHERE user='{$_POST['user']}'
AND pass='$passwd'";
$result = mysql_query($sql);
// Didn't find username and password
if (mysql_num_rows($result) == 0){
header("Location: login.php?badlogin=");
exit;
}
// set session with unique index
$_SESSION['sess_id'] = mysql_result($result, 0, 'id');
$_SESSION['sess_user'] = $_POST['user'];
// Log the logins
// $REMOTE_ADDR = PHP variable to get ip address
$visitor_ip = $_SERVER["REMOTE_ADDR"];
$sql = "INSERT INTO userlog(user, date, ipadress)
VALUES('{$_POST['user']}', CURRENT_TIMESTAMP, '{$visitor_ip}')";
mysql_query($sql);
header("Location: valjtv.php");
exit;
}
// Logout
if (isset($_GET['logout'])){
session_unset();
session_destroy();
header("Location: login.php");
exit;
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Login</title>
<link rel="stylesheet" type="text/css" media="print" href="css/reset.css">
</head>
<body bgcolor="#C0C0FF">
<?php
// If not logged in, let log in, else logut link
if (!isset($_SESSION['sess_user'])){
echo '
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<th><img src="img/Ikon.png" alt="" width="264" height="175" /></th>
</tr>
</table>
<div id="login-box" align="center">
<H2>Welcome!</H2>
<p>Log in here.</p>
<form action="login.php" method="post">
Username:<br>
<input type="text" name="user"><br>
Password:<br>
<input type="password" name="passwd"><br><br>
<input type="submit" name="submit" value="Log in">
</form>
</div>
';
// Wrong login message
if (isset($_GET['badlogin'])){
echo '<div id="fail-box">';
echo "Wrong username or password.<br>\n";
echo "Try Again.\n";
echo '</div>';
}
?>
<div id="main">
<?php
} else {
header("Location: valjTV.php");
?>
<?php } ?>
</body>
</html>
I’d like to make more sessions. I have checked the php manual, but I can’t make it work with session_name(), I’m probably doing it wrong.
What I have now is login1.php and login2.php. Each one connects to a different table in a mySQL database, but they both use the same session. Login2 is pretty much the same script with a few changed constants. So if user1, while being logged into session1, types in a link that’s supposed to be accessible only by user2, he accesses the webpage because it’s the same session.
I figure one has to name the sessions different names in order to make “separate” logins. I need two (eventually more) separate sets of users (mySQL tables) who are allowed access to two separate sets of web pages, ergo I need two seperate sessions.
I’ll appreciate any help