שלום לכולם!
אני עובד כרגע על מערכת ניהול קטנה ואני מנסה לעשות שכאשר אני לא מחובר למערכת, אז לא יהיה ניתן להכנס חופשי אל המערכת ואני לא מצליח.
נתחיל מהדף login, שאותו עשיתי עם סיישן.
PHP קוד:
<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> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
הדף checklogin.php:
PHP קוד:
<?php
$host="localhost"; // Host name
$username="zvaim_new"; // Mysql username
$password="000000"; // Mysql password
$db_name="zvaim_new"; // Database name
$tbl_name="members"; // 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'];
// 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 $tbl_name 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==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
header("location:index.php");
}
else {
echo "Wrong Username or Password";
}
?>
הדף logout.php
PHP קוד:
<?
session_start();
session_destroy();
?>
והדף הראשי של המערכת (מה שאני רוצה לעשות שאם אני לא מחובר אל המערכת, אז לא יהיה ניתן להכנס לדף הראשי של המערכת, אלא הוא יעביר אותי אוטומטית אל הדף login.php.
index.php - הדף הראשי של המערכת
PHP קוד:
<?
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="rtl">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1255" />
<link rel="stylesheet" type="text/css" href="style.css" />
<title>CMS</title>
</head>
<body>
<div class="wrep">
<div id="header">
<img src="images/bar.png" width="900" height="130" alt="" />
<div id="menu">
<div class="menu_text">
<a href="index.html">דף ראשי</a>
<br />
<a href="edit.php">עריכת דפים</a>
<br />
<a href="edit.php">עריכת חבילות</a>
<br />
<a href="logout.php">התנתק</a>
</div>
</div>
</div>
<div id="tail_right">
<div class="title_text" style="margin-top: 15px;">
ברוך הבא<br />
</div>
</div>
</div>
</body>
</html>