página de login php + SESSÂO Exemplo 2
Autor: Eduardo Vieira | Publicado: 2014-06-16T11:55:00-07:00

Olá galera do PHP!!!
Hoje vamos criar uma formulário de login a onde o usuário tem que se logar para ter acesso ao painel ou seu perfil, mas primeiro vamos entender como isso vai funcionar.
A imagem abaixo mostra como isso funciona:



Bom a imagem não esta muito boa mas acho que da pra entender o que vamos fazer...
crie uma tabela no seu banco dedados mysql chamada&nbsp; "users_db"

CREATE&nbsp;TABLE&nbsp;`users_db`&nbsp;(&nbsp;&nbsp;`IdUser`&nbsp;int(11)&nbsp;unsigned&nbsp;NOT&nbsp;NULL&nbsp;AUTO_INCREMENT,&nbsp;&nbsp;`username`&nbsp;varchar(50)&nbsp;DEFAULT&nbsp;NULL,&nbsp;&nbsp;`password`&nbsp;varchar(10)&nbsp;DEFAULT&nbsp;NULL,&nbsp;&nbsp;PRIMARY&nbsp;KEY&nbsp;(`IdUser`))&nbsp;ENGINE=MyISAM&nbsp;DEFAULT&nbsp;CHARSET=latin1

Vamos ao php agora...
1º Passo
Criar o arquivo de conexão "conexao.php" 

&lt;?php$hostname_data = 'localhost';$database_data = 'test';&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #Nome do db$username_data = 'root';&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #Usuário do banco$password_data = '123';&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #Senha do Banco$cnn_data = mysql_connect($hostname_data, $username_data, $password_data) or trigger_error(mysql_error(),E_USER_ERROR);
?&gt;
Informe os dados de conexão que o seu banco de dados usa e em seguida salve e esta pronto o seu arquivo de conexão...

2º Passo
Criar a página de Login "index.php"
&lt;?php/*&nbsp;&nbsp;&nbsp; Informações de Código&nbsp;&nbsp;&nbsp; Desenvolvido por Eduardo Vieira&nbsp;&nbsp;&nbsp; Data:16/06/2014&nbsp;&nbsp;&nbsp; Email:eduardocobain@bol.com.br*/&nbsp;&nbsp;&nbsp; include('conexao.php');&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (!isset($_SESSION)) {&nbsp;&nbsp;&nbsp; &nbsp; session_start();&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; #Declarar as variaveis como nulas&nbsp;&nbsp;&nbsp; $_SESSION['MM_Login']&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; = false;&nbsp;&nbsp;&nbsp; $_SESSION['MM_Username'] &nbsp;&nbsp;&nbsp; = NULL;&nbsp;&nbsp;&nbsp; $_SESSION['MM_UserGroup'] &nbsp;&nbsp;&nbsp; = NULL;&nbsp;&nbsp;&nbsp; unset($_SESSION['MM_Username']);&nbsp;&nbsp;&nbsp; unset($_SESSION['MM_UserGroup']);&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (isset($_POST['bnt'])) {&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; #Informações para o processamento do login&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; $loginUsuario&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; = $_POST['txtuser'];&nbsp;&nbsp;&nbsp; #Recebe o valor do Campo Usuário&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; $LoginSenha&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; = $_POST['txtpwd']; &nbsp;&nbsp;&nbsp; #Recebe o valor do Campo Senha&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; $Redirecionar_Confirmado&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; = "home.php"; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; #Direciona para a pagina quando o login e confirmado&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; $Redirecionar_NãoComfirmado &nbsp;&nbsp;&nbsp; = "error.php"; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; #Direciona para a pagina quando o login não e confirmado&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; #Selecionar o banco de dados&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; mysql_select_db($database_data);&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; #Fazer a pesquisa na tabela&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; $sql_Login ="SELECT username, password FROM users_db WHERE username='".$loginUsuario."' AND password='".$LoginSenha."'";&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; $rs_login = mysql_query($sql_Login, $cnn_data) or die(mysql_error());&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; $rs_Login_Linhas = mysql_num_rows($rs_login);&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; #Validar o login&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if($rs_Login_Linhas){&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; #Opcional Variavel de Grupo e Nome do Usuário&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; $loginGrupo = "";&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; $loginNome&nbsp; = "";&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; #Declarar as variaveis&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; $_SESSION['MM_Login']&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; = true;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; $_SESSION['MM_Username'] &nbsp;&nbsp;&nbsp; = $loginUsuario;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; $_SESSION['MM_UserGroup'] &nbsp;&nbsp;&nbsp; = $loginGrupo;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; #Redirecionar a pagina&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; header("Location: ". $Redirecionar_Confirmado);&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; else&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; #Declarar as variaveis&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; $_SESSION['MM_Login']&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; = false;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; $_SESSION['MM_frmLogin']&nbsp;&nbsp;&nbsp; = "index.php";&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; $_SESSION['MM_Erro']&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; = "Erro ao realizar o login verifique o usu&amp;aacute;rio e senha...";&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; #Redirecionar a pagina&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; header("Location: ". $Redirecionar_NãoComfirmado);&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }?&gt;&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;&lt;html&gt;&lt;head&gt;&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;&lt;title&gt;Login&lt;/title&gt;&lt;/head&gt;&lt;body&gt;&lt;form name="form1" method="post" action="index.php"&gt;&nbsp; &lt;table width="200" border="0" cellpadding="2" cellspacing="0"&gt;&nbsp;&nbsp;&nbsp; &lt;tr&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;td align="left"&gt;&lt;b&gt;Usuário:&lt;/b&gt;&lt;/td&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;td align="left"&gt;&lt;input type="text" name="txtuser" id="txtuser"&gt;&lt;/td&gt;&nbsp;&nbsp;&nbsp; &lt;/tr&gt;&nbsp;&nbsp;&nbsp; &lt;tr&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;td align="left"&gt;&lt;b&gt;Senha:&lt;/b&gt;&lt;/td&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;td align="left"&gt;&lt;input type="password" name="txtpwd" id="txtpwd"&gt;&lt;/td&gt;&nbsp;&nbsp;&nbsp; &lt;/tr&gt;&nbsp;&nbsp;&nbsp; &lt;tr&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;td align="left"&gt;&lt;b&gt;&lt;/b&gt;&lt;/td&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;td align="left"&gt;&lt;input type="submit" name="bnt" id="bnt" value="Entrar"&gt;&lt;/td&gt;&nbsp;&nbsp;&nbsp; &lt;/tr&gt;&nbsp; &lt;/table&gt;&lt;/form&gt;&lt;/body&gt;&lt;/html&gt;
3º Passo
Criar a página&nbsp; "error.php"

&lt;?php/*&nbsp;&nbsp;&nbsp; Informações de Código&nbsp;&nbsp;&nbsp; Desenvolvido por Eduardo Vieira&nbsp;&nbsp;&nbsp; Data:16/06/2014&nbsp;&nbsp;&nbsp; Email:eduardocobain@bol.com.br*/&nbsp;&nbsp;&nbsp; if (!isset($_SESSION)) {&nbsp;&nbsp;&nbsp; &nbsp; session_start();&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp; if($_SESSION['MM_Login']=="" || $_SESSION['MM_Login']==NULL || $_SESSION['MM_Login']==false){&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; #Declarar as variaveis&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; $_SESSION['MM_Login']&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; = false;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; $_SESSION['MM_Username'] &nbsp;&nbsp;&nbsp; = NULL;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; $_SESSION['MM_UserGroup'] &nbsp;&nbsp;&nbsp; = NULL;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; unset($_SESSION['MM_Username']);&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; unset($_SESSION['MM_UserGroup']);&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; $Redirecionar = $_SESSION['MM_frmLogin'];&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }?&gt;&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;&lt;html&gt;&lt;head&gt;&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;&lt;title&gt;Error&lt;/title&gt;&lt;/head&gt;&lt;body&gt;&lt;p&gt;&lt;?php echo $_SESSION['MM_Erro'];?&gt;&lt;/p&gt;&lt;a href="&lt;?php echo $Redirecionar; ?&gt;"&gt;Voltar&lt;/a&gt;&lt;/body&gt;&lt;/html&gt;

4º Passo
Criar a pagina do Painel ou perfil "home.php"
&lt;?php/*&nbsp;&nbsp;&nbsp; Informações de Código&nbsp;&nbsp;&nbsp; Desenvolvido por Eduardo Vieira&nbsp;&nbsp;&nbsp; Data:16/06/2014&nbsp;&nbsp;&nbsp; Email:eduardocobain@bol.com.br*/if (!isset($_SESSION)) {&nbsp; session_start();}if($_SESSION['MM_Login']=="" || $_SESSION['MM_Login']==NULL || $_SESSION['MM_Login']==false){&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; #Declarar as variaveis&nbsp;&nbsp;&nbsp; $_SESSION['MM_Login']&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; = false;&nbsp;&nbsp;&nbsp; $_SESSION['MM_Erro']= "Usuário não esta Logado..";&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; #Caso o tenha feito o login então enviar para a pagina de erro.&nbsp;&nbsp;&nbsp; $Redirecionar_NãoComfirmado &nbsp;&nbsp;&nbsp; = "error.php";&nbsp;&nbsp;&nbsp; header("Location: ". $Redirecionar_NãoComfirmado);}?&gt;&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;&lt;html&gt;&lt;head&gt;&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;&lt;title&gt;Home&lt;/title&gt;&lt;/head&gt;&lt;body&gt;&lt;p&gt;Página home&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;
Se você tentar acessar a pagina home direto sem ter feito o login ele redireciona para a pagina error.php

Bom é isso ai espero que tenha ajudado... até mais