Skip to content

Instantly share code, notes, and snippets.

@crystianwendel
Created November 8, 2013 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crystianwendel/7371457 to your computer and use it in GitHub Desktop.
Save crystianwendel/7371457 to your computer and use it in GitHub Desktop.
Exemplo beeem simples com controle de acesso para usuários autenticados.
<?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>
<?php
session_start();
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$email = $_POST["email"];
$password = $_POST["password"];
if ($email == 'crystian.wendel@gmail.com' 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>
<?php
session_start();
session_destroy();
header("Location: index.php");
die();
?>
<?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