Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Bom esse é meu primeiro script desenvolvido do zero em PHP e MySQL(com exceção do script security.php). Estou postando porque gostaria de saber melhorias para ele.
index.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="[http://www.w3.org/1999/xhtml"](http://www.w3.org/1999/xhtml) dir="ltr" lang="pt-BR">
<head>
<title>LOGIN</title>
</head>
<body>
<form action="login.php" method ="post">
<fieldset>
<legend> :: Login :: </legend>
<label for="login">Nick: </label>
<input name="login" id="login" type="text" /><br />
<label for="passwd">Pass: </label>
<input name="passwd" id="passwd" type="password" /><br />
<input type="submit" name="logar" id="logar" value="Entrar!" />
</fieldset>
</form>
<h2><a href="cadastro.php">Cadastre-se</a></h2>
</body>
</html><?php
include("conexao.php");
include("security.php");
extract($_POST);
if(isset($logar)){
session_start();
mysql_select_db('espec',$conexao);
$sql = "SELECT * FROM login WHERE login='" . anti_sql_injection($login) . "' and passwd='" . anti_sql_injection($passwd) . "'";
$rs = mysql_query($sql) or die ("Login inválido ".mysql_error());
if(mysql_num_rows($rs)>0){
$_SESSION['login'] = $login;
header("Location: pagina.php?logged=$login");
}
else {
print "<h2>Usuário e/ou senha inválido(s).</h2>";
}
}
?>
conexao.php
<?php
$conexao = mysql_connect("localhost","root","") or die("Impossível de se conectar ao BD" .mysql_error());
?>
security.php(script pronto)
<?php
function anti_sql_injection($string)
{
$string = get_magic_quotes_gpc() ? stripslashes($string) : $string;
$string = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($string) : mysql_escape_string($string);
return $string;
}
?>
cadastro.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="[http://www.w3.org/1999/xhtml"](http://www.w3.org/1999/xhtml) dir="ltr" lang="pt-BR">
<head>
<title>CADASTRO</title>
</head>
<body>
<form action="signin.php" method ="post">
<fieldset>
<legend> :: Login :: </legend>
<label for="login">Nick: </label>
<input name="login" id="login" type="text" /><br />
<label for="passwd">Pass: </label>
<input name="passwd" id="passwd" type="password" /><br />
<input type="submit" name="cadastrar" id="cadastrar" value="Cadastrar!" />
</fieldset>
</form>
</body>
</html><?php
include("conexao.php");
include("security.php");
extract($_POST);
if(isset($cadastrar))
{
if(!empty($login) and !empty($passwd))
{
mysql_select_db('espec',$conexao);
$sql = "SELECT * FROM login WHERE login='" . anti_sql_injection($login) . "'";
$rs = mysql_query($sql) or die ("Login inválido ".mysql_error());
if(mysql_num_rows($rs)==0){
mysql_query("INSERT INTO login (login, passwd) VALUES ('" . anti_sql_injection($login) . "', '" . anti_sql_injection($passwd) . "')");
echo "cadastrado com sucesso!";
}
else
{
echo "usuário já existente";
}
}
else
{
echo "favor preencher campos";
}
?>
Uffa... é isso aí, valeu pra quem criticar =p
Carregando comentários...