מדריך למערכת התחברות בסיסית (עם OOP ו PDO)
שלום, במדריך הזה אני יכין מערכת התחברות בסיסית ב PHP עם OPP ו PDO.
אז שנתחיל ?
קודם כל אנחנו צריכים להכין מסד נתונים, אז נכין אחד בשם users וטבלא בשם login
בתוך המסד נכניס שם וסיסמה לדוגמא כדי שאחרי זה נוכל להתחבר דרכם.
ובתוך טבלא זאת נשים את הפרטים הבאים :
עכשיו אחרי שהכנו את המסד נתונים, נתחיל לכתוב קוד !
עכשיו נכין דף חדש בשם index.php ונתחיל לכתוב שם קוד :
קודם כל הכנו דף תקין של HTML.
קוד:
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1" />
<title>by visualmode - Iatraf</title>
</head>
<body>
</body>
</html>
עכשיו אנחנו נכין טופס שיש שני INPUT שאחד קולט את השם משתמש והשני את הסיסמה :
קוד:
<form method="post" action="index.php">
username : <input type="text" name="user" /> <br />
password : <input type="password" name="pass" /> <br />
<input type="submit" name="submit" value="login" />
</form>
ועכשיו כמובן הכול ביחד :
קוד:
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1" />
<title>by visualmode - Iatraf</title>
</head>
<body>
<form method="post" action="index.php">
username : <input type="text" name="user" /> <br />
password : <input type="password" name="pass" /> <br />
<input type="submit" name="submit" value="login" />
</form>
</body>
</html>
לעכשיו סיימנו את הדף.
בוא נפתח דף חדש עכשיו בשם : classes.php
קודם כל בהתחלה אנחנו נכין לנו CLASS בשם "connection" ששם יהיה מתודה שתחזיר לנו את הערך של ההתחברות :
קוד:
<?php
class connection { // creating a class
public function dbconnect() { // creathing a method
return new PDO("TTT=XXX; dbname=YYY" ,"ZZZ", "VVV"); // returning connection
}
}
-בתוך ה TTT אנחנו נכתוב איזה סוג של דריבר זה יריץ למשל mysql
-בתוך ה YYY אנחנו נכתוב את שם המסד נתונים
-בתוך ה ZZZ אנחנו נכתוב את השם משתמש של המסד
-בתוך ה VVV נכתוב את הסיסמה של המסד.
עכשיו אחרי שיש לנו מתוד שמחזיר לנו את ההתחברות אנחנו נכין עוד CLASS בשם "user" שתעשה את ההתחברות.
אנחנו קודם כל נכין משתנה פרטי שיכיל לנו את ההתחברות למסד, ואז עם __construct אנחנו נגדיר שקוראים ל CLASS זה ישים אוטומטית את ההתחברות של המסד למשתנה :
קוד:
class User {
private $db;
public function __construct() {
$this->db = new connection;
$this->db = $this->db->dbconnect();
}
}
בתוך המתודה CONSTRUCT זה מכין אובייקט חדש של ההתחברות של הCLASS הקודם שהכנו, ואז קורא למתוד של ההתחברות ומגדיר את המשתנה עליו.
עכשיו נוסיף עוד מתודה בשביל הנוכות שלוקחת טקסט ואז עושה הודעה קופצת עם JS (לא חובה לי יותר נוח) :
קוד:
public function alert($text) {
echo '<script type="text/javascript">alert("'.$text.'")</script>';
}
מה שיש לנו עד עכשיו בעמוד :
קוד:
<?php
class connection { // creating a class
public function dbconnect() { // creathing a method
return new PDO("mysql:host=127.0.0.1; dbname=users" ,"root", ""); // returning connection
}
}
class User {
private $db;
public function __construct() {
$this->db = new connection;
$this->db = $this->db->dbconnect();
}
public function alert($text) {
echo '<script type="text/javascript">alert("'.$text.'")</script>';
}
}
טוב עכשיו אחרי שהכנו את הההתחברות למסד ואת ההודעה הקופצת, מתחיל לכתוב את הקוד שיאמת את ההתחברות :
עכשיו אנחנו נכין מתוד, שיקח שני פרמטרים "name","pass" הוא יבדוק אם הם ריקים ואם כן יחזיר בעיה ואם לא ימשיך, אחרי זה אני יכין שאילתה, אז יכניס לה 2 נתונים (שם משתמש והסיסמה) אחרי זה אני יפעיל את השאילה, ויבדוק אם יש שם משתמש וסיסמה כאלה אם יש אז זה יתחבר ואם לא אז לא :
קוד:
public function login($name,$pass) {
if (empty($name) || empty($pass)) {
$this->alert("please fill in all fields");
} else {
$query = $this->db->prepare("SELECT * FROM `login` WHERE name=? AND pass=?");
$query->bindParam(1, $name);
$query->bindParam(2,$pass);
$query->execute();
if($query->rowCount() == 1)
$this->alert("succefully logged in");
else
$this->alert("incorrect username or password");
}
}
עכשיו כל הקוד מסוכם בעמוד הזה, גם מי שלא הבין שמתי הארות בצד על מה כל דבר עושה :
קוד:
<?php
class connection { // creating a class
public function dbconnect() { // creathing a method
return new PDO("mysql:host=127.0.0.1; dbname=users" ,"root", ""); // returning connection
}
}
class User { // creating a class user
private $db; // var db for connection
public function __construct() { // method construct
$this->db = new connection; // creat a new connection
$this->db = $this->db->dbconnect(); // putting in a var the connection
}
public function login($name,$pass) { // login method taking 2 param name and pass
if (empty($name) || empty($pass)) { // checking if one of them is empty
$this->alert("please fill in all fields"); // if it is alerting error
} else { // if its not empty
$query = $this->db->prepare("SELECT * FROM `login` WHERE name=? AND pass=?"); // prepare a query
$query->bindParam(1, $name); // puts the username is the query
$query->bindParam(2,$pass); // puts the password in the query
$query->execute(); // execute the query
if($query->rowCount() == 1) // checks if a user exites
$this->alert("succefully logged in"); // if does alert succed
else //if not exites
$this->alert("incorrect username or password"); // alery failed
}
}
public function alert($text) { // method alert taking the text
echo '<script type="text/javascript">alert("'.$text.'")</script>'; // echo an alert box with the text
}
}
עכשיו כל מה שנשאר לנו לעשות זה בעמוד INDEX.PHP להוסיף בדיקה שאם לחצו על הכפתור זה יעשה פעולה(נוסיף את זה למלא בדף INDEX) :
קוד:
<?php
require_once("classes.php");
if (isset($_POST['submit'])) {
$user = $_POST['user'];
$pass = $_POST['pass'];
$object = new User;
$object->login($user,$pass);
}
?>
כל הדף עם הארות למי שעדין לא הבין :
קוד:
<?php
require_once("classes.php");// require the classes page
if (isset($_POST['submit'])) { // if clickes the button
$user = $_POST['user']; // gets username
$pass = $_POST['pass']; //gets password
$object = new User; // creates a new class
$object->login($user,$pass); // does the login method
}
?>
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1" />
<title>by visualmode - Iatraf</title>
</head>
<body>
<form method="post" action="index.php"> <!-- crates the form -->
username : <input type="text" name="user" /> <br />
password : <input type="password" name="pass" /> <br />
<input type="submit" name="submit" value="login" />
</form>
</body>
</html>
טוב אז זה סוף המדריך !
הורדה למי שלא הצליח (בתיקיית DB יש את כל ה מסד) :
http://www.upf.co.il/file/870218473.html
סיסמה : iatraf
אני עבדתי על זה שעתיים וחצי, הכל אני כתבתי, אני אשמח לתגובות, למי שלא הבין או שיש לו שאלה אשמח אם הוא אשאל 
תגוובת לא עולות כסף !
כל הזכויות שמורות לי - אין להעתיק ללא רשות