Du så ikke min kode?
login.php
- <?php
- session_start();
- require_once("db.php");
-
- if(isset($_SESSION["admin"])) {
- header("Location: admin.php");
- }
-
- if(isset($_POST["login"])) {
- $error = false;
- $fejlbesked = "";
-
- $username = mysql_real_escape_string($username);
-
- $crypt = sha1($_POST["password"]);
- $password = mysql_real_escape_string($crypt);
-
- if(!empty($username)) {
- $error = false;
- } else {
- $error = true;
- $fejlbesked .= "Du skal udfylde dit brugernavn!<br />";
- }
-
- if(!empty($password)) {
- $error = false;
- } else {
- $error = true;
- $fejlbesked .= "Du skal udfylde dit password!<br />";
- }
-
- if($error == true) {
- echo $fejlbesked;
- } else {
- $sql = "SELECT username FROM users WHERE username = '". $username ."' AND password = '". $password ."'";
- $query = mysql_query($sql);
-
- if(!$query) {
- die(mysql_error());
- } else {
- $row = mysql_fetch_assoc($query);
- $_SESSION["admin"] = 1;
- $_SESSION["username"] = $row["username"];
- header("admin.php");
- }
- }
- }
- ?>
-
- <form method="post" action="login.php">
- Brugernavn: <input type="text" name="username" /><br />
- Password: <input type="password" name="password" /><br />
- <input type="submit" name="login" value="Log ind" />
- </form>
SQL
- CREATE TABLE `users` (
- id INT NOT NULL AUTO_INCREMENT,
- PRIMARY KEY (id),
- username VARCHAR(60),
- password VARCHAR(60)
- )
logud.php
- session_start();
- require_once("db.php");
-
- if(isset($_SESSION["admin"])) {
- session_destroy();
- header("Location: login.php");
- } else {
- header("Location: login.php");
- }
admin.php
- session_start();
- require_once("db.php");
-
- if(isset($_SESSION["admin"])) {
- echo 'Velkommen ' . $_SESSION["username"] . '<br />';
- echo 'Ønsker De at logge ud? Så klik her: <a href="logud.php">log ud</a>';
- } else {
- echo 'Dette er en beskyttet side!';
- }
db.php
- $dbhost = "localhost"; // ret selv til
- $dbuser = ""; // ret selv til
- $dbpass = ""; // ret selv til
- $dbname = ""; // ret selv til
-
- $connection = mysql_connect($dbhost, $dbuser, $dbpass);
- $db = mysql_select_db($dbname, $connection);
-
- if(!$db) {
- die(mysql_error());
- }
P.S. Koderne er ikke testet!
Indlæg senest redigeret d. 21.12.2011 10:49 af Bruger #16025