1
I’m having problems in the login system, the fact is in one of the pages of the system in particular on the page perfil.php
.
To explain better I will show the whole login system process.
Page cadastro.php
:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<link rel="stylesheet" type="text/css" href="css/estilo.css"/>
<head>
<meta http-equiv="content-Type" content="text/html; charset=iso-8859-1" />
<title>cadastrar</title>
</head>
<body>
<div class="menu">
<ul>
<li>
<a href="index.php">Página Inicial</a>
</li>
</ul>
</div>
<div class="tabela">
<form action=" cadastro_db.php" method="post" id="form1" enctype="multipart/form-data">
<table width="200" border="0">
<tr >
<td class="tabelinha" colspan="2">Cadastrar
</td>
</tr>
<tr>
<td class="tabelinha" width="80">Nome</td>
<td class="tabelinha" width="150"><label>
<input type="text" name="nome" id="nome" />
</label></td>
</tr>
<tr>
<td class="tabelinha" width="80">Email</td>
<td width="150" class="tabelinha" id="senha"><input type="text" name="email" id="email" /></td>
</tr>
<tr>
<tr>
<td class="tabelinha" width="80">Idade</td>
<td class="tabelinha" width="150"><label>
<input type="text" name="idade" id="idade" />
</label></td>
</tr>
<tr>
<td class="tabelinha" width="80">Cidade</td>
<td width="150" class="tabelinha" id="senha"><input type="text" name="cidade" id="cidade" /></td>
</tr>
<tr>
<tr>
<td class="tabelinha" width="80">Login</td>
<td class="tabelinha" width="150"><label>
<input type="text" name="login" id="login" />
</label></td>
</tr>
<tr>
<td class="tabelinha" width="80">Senha</td>
<td class="tabelinha" width="150"><label>
<input type="text" name="senha" id="senha" />
</label></td>
</tr>
<tr>
<td class="tabelinha" width="80">Imagem</td>
<td class="tabelinha" width="150"><label>
<input type="file" name="foto" id="foto" />
<input type="hidden" name="acao" value="cad"/>
</label></td>
</tr>
<tr>
<td colspan="2"><label>
<div align="center">
<br /> <input type="submit" name="enviar" id="enviar" value="cadastrar" /><br />
</div>
</label></td>
</tr>
</table>
</form>
</div>
</body>
</html>
Page cadastro_db.php
:
<?php
include("connection.php");
$nome=$_POST['nome'];
$email=$_POST['email'];
$idade=$_POST['idade'];
$cidade=$_POST['cidade'];
$login=$_POST['login'];
$senha=$_POST['senha'];
if(!is_file($_FILES['foto'] ['tmp_name'])){
echo"
<meta http-equiv='refresh' content= '0; url=cadastro.php'/>
<script type = 'text/javascript'>alert ('Selecione uma imagem')</script>
";
}
else {
$foto=$_FILES['foto'] ['name'];
$foto= str_replace("","",$foto);
$foto= str_replace("à","a",$foto);
$foto= str_replace("á","a",$foto);
$foto= str_replace("á","a",$foto);
$foto= str_replace("é","e",$foto);
$foto= str_replace("è","e",$foto);
$foto= str_replace("ì","i",$foto);
$foto= str_replace("í","i",$foto);
$foto= str_replace("ó","o",$foto);
$foto= str_replace("õ","o",$foto);
$foto= str_replace("õ","o",$foto);
$foto= str_replace("ç","c",$foto);
$foto= str_replace("ù","u",$foto);
$foto= str_replace("û","u",$foto);
$foto= str_replace("ü","u",$foto);
$foto=strtolower($foto);
$tipos = array("image/pjeg", "image/jpeg", "image/gif", "image/pjeg", "image/png", "image/jpg" );
$arqType = $_FILES['foto'] ['type'];
if(array_search($arqType, $tipos)== false){
echo " <meta http-equiv='refresh' content= '0; url=index.php'>
<script type = 'text/javascript'>alert ('Formato Inválido')</script>";
}
else{
if(file_exists("fotos/$foto")){
$a = 1;
while (file_exists("fotos/($a)$foto")){
$a++;
}
$foto= "[".$a."]$foto";
}
if(!move_uploaded_file($_FILES['foto'] ['tmp_name'],"fotos/".$foto)){
echo " <meta http-equiv='refresh' content= '0; url=index.php'>
<script type = 'text/javascript'>alert ('falha!')</script>";
}
$sql = mysqli_query($db, "INSERT INTO usuario(nome, email, idade, cidade, login, senha, foto) VALUES ('$nome', '$email', '$idade', '$cidade', '$login', '$senha', '$foto')");
// Fecha a conexão antes de redirecionar
header("Location: index.php");
}
mysqli_close($db);
}
?>
Page connection.php
:
<?php
$db = mysqli_connect("localhost", "root", "angola");
mysqli_select_db($db, "login_senha");
?>
Page index.php
:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="css/estilo.css"/>
<title>Entrar</title>
</head>
<body>
<div class="menu">
<ul>
<li>
<a href="#">Página Inicial</a>
</li>
<li>
<a href="cadastro.php">Cadastrar</a>
</li>
</ul>
</div>
<div class="tabela">
<form action="logar.php" method="post" id="form1" enctype="multipart/form-data">
<table width="200" border="0">
<tr >
<td class="tabelinha" colspan="2">ENTRAR
</td>
</tr>
<tr>
<td class="tabelinha" width="80">login</td>
<td class="tabelinha" width="150"><label>
<input type="text" name="login" id="login" />
</label></td>
</tr>
<tr>
<td class="tabelinha" width="80">senha</td>
<td width="150" class="tabelinha" id="senha"><input type="text" name="senha" id="senha" /></td>
</tr>
<tr>
<td colspan="2"><label>
<div align="center">
<br /> <input type="submit" name="enviar" id="enviar" value="Entrar" /><br />
</div>
</label></td>
</tr>
</table>
</form>
</div>
</body>
</html>
Page logar.php
:
<?php
$db = mysql_connect("localhost", "root", "angola") or die("cannot connect");
$con= mysql_select_db("login_senha");
$login = $_POST['login'];
$senha_post = $_POST['senha'];
$sql = mysql_query( "SELECT * FROM usuario WHERE login='$login'");
$cont = mysql_num_rows($sql);
if($cont==0){
echo"
<meta http-equiv='refresh' content= '0; url=cadastro.php'/>
<script type = 'text/javascript'>alert ('Login não existe')</script>
";
} else {
while($linha = mysql_fetch_array($sql)){
if($linha['senha']!=$senha_post){
echo "
<meta http-equiv='refresh' content= '0; url=cadastro.php'/>
<script type = 'text/javascript'>alert ('Senha não corresponde')</script>
";
} else {
$_SESSION['login_usuario']=$login;
$_SESSION['login_senha']=$senha;
header("Location: perfil.php");
}
}
}
mysql_close($db);
Page perfil.php
:
$db = mysql_connect("localhost", "root", "angola") or die("cannot connect");
$con= mysql_select_db("login_senha");
$login = $_POST['login'];
$senha = $_POST['senha'];
$enviar = $_POST['enviar'];
$sql = mysql_query( "SELECT * FROM usuario WHERE login='$login'");
while($linha = mysql_fetch_array($sql)){
$senha =$linha['senha'];
}
$cont = mysql_num_rows($sql);
if($cont==0){
echo"
<meta http-equiv='refresh' content= '0; url=cadastro.php'/>
<script type = 'text/javascript'>alert ('Login não existe')</script>
";
}else{
if( $senha_db!=$senha){
echo"
<meta http-equiv='refresh' content= '0; url=cadastro.php'/>
<script type = 'text/javascript'>alert ('Senha não corresponde')</script>
";
}else{
session_start();
$_SESSION['login_usuario']=$login;
$_SESSION['login_senha']=$senha;
header("Location: ficha.php");
}
}
mysql_close($db);
?>
Page
<?php
session_start();
mysql_connect("localhost", "root", "angola") or die("cannot connect");
mysql_select_db("login_senha");
$login = $_POST['login']; nesta linha ele parece com erro:
/*este erro: Notice: Undefined index: login in C:\xampp\htdocs\Techphp\perfil.php on line 6 */
$sql = mysql_query( "SELECT * FROM usuario WHERE login='($login)'");
$linha = mysql_fetch_array($sql);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" type="text/css" href="/Techphp/css/estilo.css"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="/Techphp/css/estilo.css"/>
<title>Untitled Document</title>
</head>
<body>
<div class="container">
<div class="menu">
<ul>
<li>
<a href="index.php">Voltar</a>
</li>
<li>
<a href="#">Administração</a>
</li>
</ul>
</div>
<?
if(mysql_num_rows($sql)==0){
?>
<div class="perfil">
<img src="fotos/<?php echo $linha['foto']; ?>" alt="imagem de perfil" title="imagem de perfil" />
</div>
<div class="dados">
<p>Nome</p><p><?php echo $linha['nome']; ?></p>
<p>Email</p><p><?php echo $linha['email'];?></p>
<p>idade</p><p>40<?php echo $linha['idade'];?></p>
<p>Cidade</p><p><?php echo $linha['cidade'];?></p>
</div>
<? } else { ?>
<div class="perfil">
Este usuário não está logado ou não foi encontrado.
</div>
<? } ?>
</div>
</body>
</html>
login='($login)' -> change to login='{$login}'
– Sr. André Baill
Change also: $login = $_POST['login']; to : if(isset($_POST['login'])){ $login = $_POST['login']; } Else { echo "Empty Login."; }
– Sr. André Baill