index.php:
- <html xmlns="http://www.w3.org/1999/xhtml">
- <html>
- <body>
- <div id="content">
-
- <div id="content_text">
- <table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="black">
- <tr>
- <form name="form1" method="post" action="checklogin.php">
- <td>
- <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#D8D8D8">
- <tr>
- <td colspan="3"><strong><center>Administrator login</center></strong></td>
- </tr>
- <tr>
- <td width="78">Username</td>
- <td width="6">:</td>
- <td width="294"><input name="myusername" type="text" id="myusername"></td>
- </tr>
- <tr>
- <td>Password</td>
- <td>:</td>
- <td><input name="mypassword" type="password" id="mypassword"></td>
- </tr>
- <tr>
- <td> </td>
- <td> </td>
- <td><input type="submit" name="Submit" value="Login" id="submit"></td>
- </tr>
- </table>
- </td>
- </form>
- </tr>
- </table>
-
- </div>
- <br/>
- <br/>
- </div>
-
- </body>
- </html>
checklogin.php:
- <?php
- ob_start();
- if(empty($_POST['myusername']) || empty($_POST['mypassword'])) {
- $fail++;
- header("location: index.php?fail=$fail");
- }
-
- include("config.php");
- // Define $myusername and $mypassword
- $myusername=$_POST['myusername'];
- $mypassword=$_POST['mypassword'];
-
- // To protect MySQL injection (more detail about MySQL injection)
- $myusername = stripslashes($myusername);
- $mypassword = stripslashes($mypassword);
- $myusername = mysql_real_escape_string($myusername);
- $mypassword = mysql_real_escape_string($mypassword);
-
- $sql="SELECT * FROM main WHERE username='$myusername' and password='$mypassword'";
- $result=mysql_query($sql);
-
- // Mysql_num_row is counting table row
- $count=mysql_num_rows($result);
- // If result matched $myusername and $mypassword, table row must be 1 row
-
- if($count>0){
- // Register $myusername, $mypassword and redirect to file "login_success.php"
- session_register("myusername");
- session_register("mypassword");
- header("location:main.php");
- }
- else {
- include("bad.php");
- }
-
- ob_end_flush();
- ?>
main.php
- <?php
- session_start();
- if(session_is_registered(myusername)){
- echo "hejsa, $_SESSION[myusername] du er nu logget ind!";
- }
- ?>
Skulle gøre det.