Mysql
 sql >> Datenbank >  >> RDS >> Mysql

Weiterleitung zur Startseite nach Anmeldung php

Dieses Beispiel sollte Ihnen ein wenig helfen, aber ich empfehle die Verwendung von mysqli und es ist eine wirklich schlechte Idee, Ihre Passwörter im Klartext zu speichern. Sie sollten erwägen, mysqli nachzuschlagen, Passwörter zu hashen und sich mit PHP 5.4 vertraut zu machen, da sich viele der hier verwendeten Funktionen geändert haben und nicht mehr unterstützt werden.

checklogin.php

<?php

$host="host"; // Host name 
$username="username"; // Mysql username 
$password="password"; // Mysql password 
$db_name="database"; // Database name 
$tbl_name="table"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// username and password sent from form 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword']; 

// Remove Magic Quotes
if (get_magic_quotes_gpc()){
  $myusername = stripslashes($myusername);
  $mypassword = stripslashes($mypassword);
}
$sql = sprintf(
  "SELECT * FROM $tbl_name WHERE username='%s' and password='%s' LIMIT 1;",
  mysql_real_escape_string($myusername),
  mysql_real_escape_string($mypassword)
);
$result = mysql_query($sql);

// MySQL count
$count = mysql_num_rows($result);

if ($count){
  $_SESSION['username'] = $myusername; // $_SESSION['loggedin'] = true or false would work too
  $_SESSION['mypassword'] = $mypassword; // Why store the password in session data?
  header("Location: login_success.php");
}else{
  header("Location: main_login.php?msg=Login_Failed");
}
?>

login_success.php

<?php
session_start();
if (!isset($_SESSION['username']) || empty($_SESSION['username']){
  header("Location: mail_login.php");
}

?>
<html>
<body>
Welcome <?php echo $_SESSION['username']; ?>
Login Successful
</body>
</html>    

main_login.php

<?php
if (isset($_GET['msg']) && !empty($_GET['msg']) echo $_GET['msg'];
?>
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="checklogin.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Member Login </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="text" id="mypassword"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>

Benutzung auf eigene Gefahr. :)