Created
November 8, 2013 14:02
-
-
Save crystianwendel/7371457 to your computer and use it in GitHub Desktop.
Exemplo beeem simples com controle de acesso para usuários autenticados.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
session_start(); | |
?> | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8" /> | |
<title></title> | |
</head> | |
<body> | |
<h1>Index page! This page is public!</h1> | |
<p>Paragraph</p> | |
<p><a href="login.php">Login!</a></p> | |
<p><a href="protectedPage.php">Protected page</a></p> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
session_start(); | |
if ($_SERVER["REQUEST_METHOD"] == "POST") | |
{ | |
$email = $_POST["email"]; | |
$password = $_POST["password"]; | |
if ($email == '[email protected]' and $password == '12345678') { | |
$_SESSION['user'] = array(); | |
$_SESSION['user']['email'] = $email; | |
header("Location: protectedPage.php"); | |
die(); | |
} else { | |
$message = 'Usuário ou senha inválidos!'; | |
} | |
} | |
?> | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8" /> | |
<title></title> | |
</head> | |
<body> | |
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post"> | |
<?php | |
if ($message) { | |
echo '<p style="color:red; border: 1px solid red;">'.$message."</p>"; | |
} | |
?> | |
<p> | |
Email: <input type="email" placeholder="E-mail" name="email" /> | |
</p> | |
<p> | |
Password: <input type="password" name="password" /> | |
</p> | |
<p> | |
<input type="submit" value="Login" /> | |
</p> | |
</form> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
session_start(); | |
session_destroy(); | |
header("Location: index.php"); | |
die(); | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
session_start(); | |
if (!$_SESSION["user"] != NULL) { | |
header("Location: index.php"); | |
die(); | |
} | |
?> | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8" /> | |
<title></title> | |
</head> | |
<body> | |
<h1>This is a protected Page! Yay!</h1> | |
<p><a href="logout.php">Logout!</a></p> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment