E por que não um sistema de login?
Why not a login system?
Isso aí pessoal depois de um tempo sem conseguir postar coisas interessantes vamos agora a um sistema de login. Sistema esse baseado em tudo o que vimos aqui no blog.
Folks after a some time without my blog, now I come back with a login system. This system is based in early learned lessons here.
Como o nosso banco de dados e as classes já estão prontos, vamos ao próximo passo que é a tela que pede os dados para login (Isso você entendeu bem, usaremos o que aprendemos até agora!).
As our data base and classes are ready, we can move onto the next step which is the screen that asks the details to log in (If you understood well, we will use what we learned until now!).
Código/Code:
<html>
<head>
<title>Login</title>
</head>
<body>
<div id="msgError">
<?php
if (isset($_GET["r"]))
if ($_GET["r"] == "1")
echo("Login e/ou senha inválidos - Login or password invalid");
?>
</div>
<form name="form1" method="post" action="loggin.php">
<table width="300">
<tr>
<th colspan="2">Login</th>
</tr>
<tr>
<th>usuário/user:</th>
<td><input name="textLogin" class="box" type="text" id="textLogin" size="20" maxlength="300"></td>
</tr>
<tr>
<th>senha/password:</th>
<td><input name="textPwd" class="box" type="password" id="textPwd" size="20" maxlength="20"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="Submit" value="Entrar/Enter"></td>
</tr>
</table>
</form>
</body>
</html>
Arquivo/File: login.php.
Como podem ver teremos agora o arquivo loggin.php citado como action no form html.
Now, you can see that we have the file loggin.php called as action in form html.
Código/Code:
<?php
/*! Verify if user exists and test their username/email and password.
* @author Danilo Lutz <danilo@infoideia.com>
* @date 15/09/2009
*/
include_once("class/connection/connection.class.php"); // Pen atention for your directories.
include_once("class/user/user.class.php");
$login = "";
$password = "";
if (isset($_POST["textLogin"]) && isset($_POST["textPwd"])) {
$login = $_POST["textLogin"];
$password = $_POST["textPwd"];
$objConnection = new Connection;
$connection = $objConnection->connect('localhost', 'root', 'vertrigo', 'teatching');
$isValid = false;
$objUser = new User($connection, null);
$isValid = $objUser->validateUser($login, $password); //Verify if is a valid user.
if ($isValid) {
session_start(); // Puts data in a Session for future access.
$_SESSION["idUser"] = $objUser->getId();
$_SESSION["name"] = $objUser->getName();
$_SESSION["email"] = $objUser->getEmail();
header("Location: youAdmPage.php"); // Redirect to your adm start page.
}
else
header("Location: login.php&r=1"); // Returns to login page, showning the error message.
$objConnection->disconnect($connection);
}
else
header("Location: login.php");
?>
Arquivo/Files: loggin.php.
Ah! Claro não poderia deixar de falar que teremos de verificar se o usuário tem acesso a página chamada, sempre no início da mesma. Vamos para a verificação:
Oh, course! I must say that we will have to verify if the user has access to the asked page always in the its beginning. Moving on to the verifying:
Código/Code:
<?php
session_start();
$logged = 0;
if (isset($_SESSION["idUser"]) || isset($_SESSION["name"])) { // Testing session for verifiy if it's was setted.
$logged = 1;
}
// If user was not logged. So will be redirected for login page.
if ($logged == 0) {
header("Location: login.php");
exit;
}
?>
Arquivo/Files: verify.php.
Segue agora, o código para utilizar o verify.php.
Here it is, the code to be used verify.php.
Código/Code:
<?php
include_once("verify.php");
?>
Espero que tenham gostado e entendido, mas cabe como execício a vocês testarem, ver se o código está funcionando e me avisarem caso não estiver, pois a tarefa de testar deixei para vocês, meus amigos! 🙂
And the last one at last: I hope you guys had enjoyed and understood, but it is totally up to you to test it, if the code is working and let me know if it isn’t, because the testing part depends only on you, my friends. 🙂
É bom, pois assim vão aprendendo a lidar com o PHP.
Yeah, then you learn how to deal with PHP.