Jump to content

[Resolvido] login nao funciona


joaocasta

Recommended Posts

Boa tarde criei este topico porque o meu sistema de login nao esta a funcionar e nao faço a minima ideia do q se passa.

Preencho os dados correctos e pronto. só que ele nao me redireciona para a pagina index.php, volta sempre para trás.

login.php


<?php

require_once "template.php";

// se vier um pedido para login
if (!empty($_POST)) {

include "config.php";

// receber o pedido de login com segurança
$username = mysql_real_escape_string($_POST['username']);
$password = md5($_POST['password']);

// verificar o utilizador em questão (pretendemos obter uma única linha de registos)
$login = mysql_query("SELECT * FROM users WHERE username = '$username' AND password = '$password'");

if ($login && mysql_num_rows($login) == 1) {


// o utilizador está correctamente validado
// guardamos as suas informações numa sessão ,pode haver mais de um username no sistema? pode, mas nao pode haver com o mesmo username
$_SESSION['id'] = mysql_result($login, 0, 0);//nao tem a ver com o nome do login_date pois nao tabela tenho login_date, entao? não lembro direito como faz esse where, procure no meu pc, no google
$_SESSION['username'] = mysql_result($login, 0, 1);

echo "conseguiste";
} else {

echo "<p>Utilizador ou password invalidos. <a href=\"login.php\">Tente novamente</a></p>";
}
}

?>

<form method="post" action="#">
<table>
<tr>
								 <td>
									Username:
									</td>
									<td>
									<input name="username" type="text" class="input" size="25"/>
									</td>
								</tr>
<tr>
								 <td>
									Password:
									</td>
									<td>
									<input name="password" type="password" class="input" size="25"/>
									</td>
								</tr>
</table>
<center><input type="submit" name="Submit" value="Entrar" /></center>
</form><a href="recoverpass.php">Recuperar a Password</a>

</body>
</html>

index.php


<?php

include "config.php"; 

session_start();
if (empty($_SESSION['username'])) {

// não existe sessão iniciada
// neste caso, levamos o utilizador para a página de login
header('Location: login.php');
exit();
}
?>

<?php

 echo "<p>Ola {$_SESSION['username']}</p>";


?>
<html>
<script type="text/javascript">

 var _gaq = _gaq || [];
 _gaq.push(['_setAccount', 'UA-23435884-1']);
 _gaq.push(['_trackPageview']);

 (function() {
   var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
   ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
   var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
 })();

</script>
<head>
<meta http-equiv="Content-Language" content="pt-br">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title> </title>
<style>
<!--
.tb{border-collapse:collapse}td{color:#000}
td{font-family:arial,sans-serif}
.tc{padding:0}.bodyline { background-color: #FFFFFF; border: 1px #98AAB1 solid; }

.forumline { background-color: #FFFFFF; border: 2px #006699 solid; }

td.row1 { background-color: #EFEFEF; }
.postbody { font-size : 12px; line-height: 18px}
.Estilo3 {color: #CCCCCC;
font-size: 12px;
font-family: Verdana, Arial, Helvetica, sans-serif;
}
.Estilo4 {color: #FFFFFF}
.style9 {
color: #FF0000;
}
.style11 {
font-size: small;
margin-top: -5px;
margin-bottom: 5px;
font-family: "Tw Cen MT";
}
.style16 {
margin-bottom: 0;
}
* {
/* Reset browsers default margin, padding and font sizes */
margin: 0;
padding: 0;
}

.style13 {
font-size: xx-small;
}
.style14 {
font-family: "Berlin Sans FB Demi";
font-size: xx-small;
}
.style12 {
color: #FF0000;
font-size: xx-small;
}
.style17 {
border-collapse: collapse;
border: 1px solid #FFFFFF;
}
.style18 {
border: 1px solid #FFFFFF;
background-color: #FFFFFF;
}
.style19 {
font-family: "Tw Cen MT";
font-size: medium;
}
.style20 {
font-family: "Tw Cen MT";
font-size: medium;
color: #006699;
margin-top: 2px;
margin-bottom: 2px;
}
.style21 {
font-size: x-small;
font-family: "Tw Cen MT";
border-width: 1px;
background-color: #FFFFFF;
}
.style22 {
color: #FF0000;
font-family: "Berlin Sans FB Demi";
}
a:active {
color: #0000FF;
}
a:hover {
color: #0000FF;
}
.style23 {
color: #006699;
}
.style26 {
font-size: x-small;
margin-top: -5px;
margin-bottom: 0;
font-family: "Tw Cen MT";
color: #FF0000;
}
.style29 {
margin-top: 2px;
margin-bottom: 2px;
font-family: "Tw Cen MT";
color: #006699;
}
.style30 {
font-size: medium;
}
.style31 {
font-size: large;
margin-top: -5px;
margin-bottom: 5px;
font-family: "Tw Cen MT";
}
.style32 {
font-family: "Tw Cen MT";
font-size: x-small;
margin-top: 2px;
margin-bottom: 2px;
}
.style33 {
font-family: "Tw Cen MT";
font-size: x-small;
margin-top: 2px;
margin-bottom: 2px;
color: #FF0000;
}
body{color:#000;margin:3px 0}body{background:#fff}.med{font-size:medium;font-weight:normal;padding:0;margin:0}#res{padding-right:1em;margin:0 16px}#res{padding-right:0 !important}#res{border:0;margin:0;padding:4px 8px 0}#center_col #res{padding-top:0}.rg_ctlv{display:inline}ul{margin:0;padding:0}.rg_ul{margin-bottom:4px;width:100%}.rg_hv{display:none;position:fixed;visibility:hidden}.rg_hv{background:#fff;border:2px solid;border-color:#ccc #666 #666 #ccc;margin:-10px;padding:8px;visibility:visible}.rg_hv{display:block;z-index:5000}.rg_hc{height:100%;overflow:hidden;width:100%}.rg_hl{outline-color:-moz-use-text-color;outline-style:none;outline-width:medium;}a:link{color:#2200C1}a:link{color:#2200C1;cursor:pointer}.rg_hi{border:0;display:block;margin:0 auto 4px}#res a img{border-color:#2200C1}-->
</style>
<meta name="google-site-verification" content="o45Zi4fHAYPRiv4laUpLSO5I5oFfEGnTQcm6NAu-SNw" />

</head>

<body "target=" vlink="#0000FF" link="#0000FF" alink="#0000FF" style="text-align: center">

<div align="center">

<div align="center">
<p style="margin-top: -10px"> </p>
<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" width="1013" height="0%" bordercolorlight="#000000" bordercolordark="#000000" bordercolor="#FFFFFF">
<!-- MSTableType="nolayout" -->
<tr>
<td align="center" bgcolor="#008000" height="24" width="1011">
 </td>
</tr>
<tr>
<td align="center" bgcolor="#008000" valign="top" style="height: 90%" width="1011">
<table border="0" cellpadding="0" style="border-collapse: collapse" width="99%">
<tr>
<td align="center" width="241" bgcolor="#FFFFFF" valign="top">
 <table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" width="212" height="186" bordercolorlight="#000000" bordercolordark="#000000" align="right">
<tr>
<td align="center" bgcolor="#000000" bordercolor="#98AAB1" width="0" height="20">
<b><font face="Vrinda" size="2" color="#FFFFFF">Destacar  
Portugual</font></b></td>
</tr>
<tr>
<td align="left" bordercolor="#000000" bgcolor="#FFFFFF" width="0" height="146">
<p align="center"><font size="1">Colocar algo em 
destaque</font></p>
<p align="center"><font size="1">Musica portuguesa</font></p>
<p align="center"><font size="1">vídeos</font></p>
<p align="center"><font size="1">Destinos</font></p>
<p align="center"><font size="1">Etc.</font></p>
<p align="center"><font size="1">(conteúdo de parcerias)</font></p>
<p align="center"><font size="1">Rotativo em imagem ou 
video</font></td>
</tr>
<tr>
<td align="center" bgcolor="#008000" width="0" height="22">
<font face="Vrinda" size="2" color="#0000FF">
<span style="text-decoration: none">
<font color="#FFFFFF">+ Mais</font></span></font></td>
</tr>
</table>
</td>
<td align="center" width="504" bgcolor="#FFFFFF">

 <p> </p>
<p> </p>
<table border="0" cellpadding="0" style="border-collapse: collapse" width="96%" height="100%">
<tr>
<td>
<div align="center">
<table border="1" width="0%" cellspacing="0" cellpadding="0" height="0%" bordercolorlight="#FFFFFF" bordercolordark="#FFFFFF">
<tr>
<td bordercolor="#FFFFFF">
<p align="center">
"Logotipo</td>
<td>"</td>
</tr>
</table>
</div>
</td>
</tr>
<tr>
<td height="53">
     <center>
       <FORM method=GET action="http://www.google.com/search?hl=pt-PT">
         <p style="margin-top: -10px"> </p>
         <TABLE bgcolor="#FFFFFF" width="386">
           <tr> 
             <td> 
<p style="margin-top: 0; margin-bottom: 0"> 
<a href="http://www.google.com/intl/pt-PT/"> 
<IMG SRC="http://www.google.com/intl/pt-PT/logos/Logo_40wht.gif" border="0" ALT="Google" align="middle" width="95" height="41"></a> 
               <INPUT TYPE=text name=q size=27 maxlength=255 value="">
               <INPUT type=submit name=btnG VALUE="Pesquisa"></td>
           </tr>
         </table>
<p style="margin-top: -2px; margin-bottom: 10px">
<span class="Estilo3">
<a title="Gmail" style="font-family: arial,sans-serif" href="http://www.gmail.com">
<font size="1">Gmail</font></a><font size="1"> -
<a style="font-family: arial,sans-serif" href="http://www.hotmail.com">
Hotmail</a> - <font color="#000000"> 
<span style="font-family: arial,sans-serif">
<a href="http://www.hi5.com">Mail Sapo</a></span></font>
-
<a style="font-family: arial,sans-serif" href="http://www.youtube.com">
YouTube</a> -
<a style="font-family: arial,sans-serif" href="http://pt.wikipedia.org/">
Wikipedia</a><span style="font-family: arial,sans-serif">   
</span><font color="#000000"> 
<a href="http://www.facebook.com">facebook</a></font> -
<a style="font-family: arial,sans-serif" href="http://www.pai.pt">
PAI</a> - <font color="#000000"> 
<a href="http://www.deco.proteste.pt/">
Deco</a></font>
-
<a style="font-family: arial,sans-serif" href="http://www.abola.pt">
A Bola</a> - </font>
<a style="font-family: arial,sans-serif" href="http://www.record.pt/">
<font size="1">Record</font></a><font size="1" color="#000000"> </font></span></p>
<p>
 </p>
       </form>
     </center>
        </td>
</tr>
</table>
</td>
<td align="center" width="256" bgcolor="#FFFFFF" valign="top">
 <table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" width="219" height="127" bordercolorlight="#000000" bordercolordark="#000000" align="left">
<tr>
<td align="center" bgcolor="#000000" bordercolor="#98AAB1" width="0" height="20">
<font face="Vrinda" size="2" color="#FFFFFF">
<b> Notícias
</b>
</font><font face="Vrinda" size="1" color="#FFFFFF">
(15/05/2012)</font></td>
</tr>
<tr>
<td align="center" width="0" bgcolor="#008000" height="82">
<div align="center">
<table border="0" width="95%" height="21">
<tr>
<td>
<p>
<font face="Vrinda" size="2" color="#FFFF00">Titulo</font><p>
<font color="#FFFFFF">
<font size="1">
Descrição</font></font><p>
<font size="1" face="Arial" color="#FFFFFF">
.</font><font size="1" face="Arial" color="#0000FF">Mais»</font></td>
</tr>
</table>
</div>
</td>
</tr>
<tr>
<td align="left" bordercolor="#000000" bgcolor="#FFFFFF" width="0" height="61">
<p style="margin-top: 0; margin-bottom: 0" align="center">
<font size="1">Anuncio</font><p style="margin-top: 0; margin-bottom: 0" align="center">
<font size="1">160x40</font></td>
</tr>
<tr>
<td align="center" bgcolor="#008000" width="0" height="0">
<font face="Vrinda" size="2" color="#0000FF">
<span style="text-decoration: none">
+<font color="#FFFFFF"> </font></span>Todas»</font></td>
</tr>
</table>
</td>
</tr>
</table>
<div align="center">
<table border="0" cellpadding="0" style="border-collapse: collapse; " width="99%" bordercolorlight="#000000" bordercolordark="#000000" bgcolor="#FFFFFF">
<tr>
<td align="center" bgcolor="#FFFFFF">
<hr>
<p><font color="#008000" face="Vrinda">Pagina inicial - <a href="area.php">Área 
Pessoal</a> - <a href="registar.php">Registar</a> - F.Q.A - Contactos - Parcerias</font></p>
<hr>
<p> </p>
<table border="0" width="92%">
<tr>
<td bgcolor="#FFFFFF">
<div align="center">
<p>Texto </p>
<p>Apresentação</p>
<p>do</p>
<p>Projecto</p>
<p> </p>
<table border="0" width="91%">
<tr>
<td align="center" width="156">
<font face="Vrinda" size="2" color="#808080">Nºs do 
"".</font></td>
<td align="center">
<font face="Vrinda" size="2" color="#808080">
Qualificações/actividades</font><p>
<font face="Vrinda" size="2" color="#808080">que vigoram 
no Pais *</font></td>
<td align="center" bordercolorlight="#000000" bordercolordark="#000000" width="156">
<font face="Vrinda" size="2" color="#808080">Produtos 
utilizados</font><p>
<font face="Vrinda" size="2" color="#808080">pelos 
Portuguese *</font></td>
<td align="center" width="156">
<font face="Vrinda" size="2" color="#808080">Contas de 
Portugal</font><p>
<font face="Vrinda" size="2" color="#808080">em € *</font></td>
<td align="center" width="156">
<font face="Vrinda" size="2" color="#808080">
Denuncias/Alertas</font><p>
<font face="Vrinda" size="2" color="#808080">dos 
cidadãos</font></td>
<td align="center" width="32">
 </td>
</tr>
<tr>
<td align="center" width="156">
<table border="1" width="0%" bordercolorlight="#000000" cellspacing="1" cellpadding="0" bordercolordark="#FFFFFF" bordercolor="#FFFFFF">
<tr>
<td>
<p align="center">
<img border="0" src="realidade.jpg" width="120" height="83"></td>
</tr>
</table>
</td>
<td align="center">
<table border="1" width="0%" bordercolorlight="#000000" cellspacing="1" cellpadding="0" bordercolordark="#FFFFFF" bordercolor="#FFFFFF">
<tr>
<td>
<p align="center">
<img border="0" src="realidade.jpg" width="120" height="83"></td>
</tr>
</table>
</td>
<td align="center" bordercolorlight="#000000" bordercolordark="#000000" width="156">
<table border="1" width="0%" bordercolorlight="#000000" cellspacing="1" cellpadding="0" bordercolordark="#FFFFFF" bordercolor="#FFFFFF">
<tr>
<td>
<p align="center">
<img border="0" src="realidade.jpg" width="120" height="83"></td>
</tr>
</table>
</td>
<td align="center" width="156">
<table border="1" width="0%" bordercolorlight="#000000" cellspacing="1" cellpadding="0" bordercolordark="#FFFFFF" bordercolor="#FFFFFF">
<tr>
<td>
<p align="center">
<img border="0" src="realidade.jpg" width="120" height="83"></td>
</tr>
</table>
</td>
<td align="center" width="156">
<table border="1" width="0%" bordercolorlight="#000000" cellspacing="1" cellpadding="0" bordercolordark="#FFFFFF" bordercolor="#FFFFFF">
<tr>
<td>
<p align="center">
<img border="0" src="realidade.jpg" width="120" height="83"></td>
</tr>
</table>
</td>
<td align="center" width="32">
<table border="1" width="72%" bordercolorlight="#000000" cellspacing="1" cellpadding="0" bordercolordark="#FFFFFF" bordercolor="#FFFFFF">
<tr>
<td>
<p align="center">
 <p align="center">
<font face="Vrinda" color="#006699" size="4">»</font><p align="center">
 </td>
</tr>
</table>
</td>
</tr>
</table>
</div>
<p align="center">
<font face="Vrinda" color="#808080" size="2">* Os Dados 
apresentados em forma de valores totais recolhidos dos 
utilizadores activos registado no PortugalO.net</font></p>
<hr>
<p align="center"><font face="Vrinda" color="#FF0000">
Anúncios "banner"</font></p>
<p align="center"><font face="Vrinda" color="#808080">
Parceiros:</font></p>
<p align="center"><font face="Vrinda">Parcerias 
rotativas (5/6 links) paginas de Portugal -  troca 
de links</font></p>
</td>
</tr>
</table>
</td>
</tr>
</table>
<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" width="100%" height="24">
<tr>
<td>
<p align="center" style="margin-top: 0; margin-bottom: 0">
<font size="2" color="#FFFF00">Pagina inicial - Contactos - 
Parcerias - Entrar na Área Pessoal - <a href="registar.php">Registar</a> - Termos</font></td>
</font></td>
</tr>
</table>
</div></td>
</tr>
</table>
</div>
</div></body>

</html>
Edited by joaocasta
Link to comment
Share on other sites

qual destas mensagem te aparece depois de tentares fazer login :

echo "conseguiste";
} else {
echo "<p>Utilizador ou password invalidos. <a href=\"login.php\">Tente novamente</a></p>";

se for a primeira :

- como sabes que não está a aceitar o login ?

se for a segunda :

altera o código para visualizar o que se está a passar

// ver o que o script está a receber do pedido
print_r($_POST);
$username = mysql_real_escape_string($_POST['username']);
$password = md5($_POST['password']);

// verificar se o SQL está correctamente criado
$sql = "SELECT * FROM users WHERE username = '$username' AND password = '$password'";
echo $sql."<br />\n";

// verificar o utilizador em questão (pretendemos obter uma única linha de registos)
$login = mysql_query($sql);

// validar o pedido à base de dados
if ($login == null)
{
 echo "Erro de base de dados : ";
 echo mysql_error();
}
IRC : sim, é algo que ainda existe >> #p@p
Link to comment
Share on other sites

para verificar se já fez login basta verificar se existe a informação guardada em sessão no momento do login

session_start();

// lembra-te que guardaste informação em $_SESSION['id']
if (isset($_SESSION['id']))
{
 // com login efetuado
}
else
{
 // val dar uma volta
}
IRC : sim, é algo que ainda existe >> #p@p
Link to comment
Share on other sites

o caracter ! é um operador unário.

assim como existem operadores binários como o + ou o - que necessitam de dois operadores (estes fazem a adição e a subtração respectivamente entre dois números), existem operadores unários que só necessitam de 1 único operador (1 número por exemplo).

Nota : existe um operador ternário mas não é para aqui chamado.

o operador ! é um operador que tem a seguinte tabela de verdade

 x  |  resultado
-----+------------
 0  |   1
 1  |   0

isto significa se o operador tiver o valor de verdadeiro, o resultado é falso e vice-versa.

no caso do PHP, todos os valores que sejam

- valor : falso

- valor : 0

- valor : null

- valor : string vazia

são considerados como falso, sendo o resultado da operação unária ! o valor booleano inverso desse.

no exemplo que deste tens:

if (isset($_SESSION['id'])) {

tens : se a variável $_SESSION['id'] existir então ...

ou

if (!isset($_SESSION['id'])) {

tens : se a variável $_SESSION['id'] não existir então ...

isto porque trocas o valor booleano do resultado da chamada da função isset

IRC : sim, é algo que ainda existe >> #p@p
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.