Login usuarios con PHP y MYSQL

Buenas a todos..

Estoy intentando realizar un login de usuarios pero no hay manera de que valla. El problema es que siempre me manda a la pagina del registro y eso lo debe hacer si el nombre de usuario y contraseña no estan en la base de datos para que te registres.

Os enseño el codigo a ver si por favor alguien puede echarme una mano..

CODIGO PHP

<?php
session_start();

include('bd.php');

$conexion=conectabd();

$nick=$_POST["nick"]; //recoge datos del formulario
$pass=$_POST["pass"]; //recooge datos del formulario

$_POST["nick"] = trim($_POST["nick"]); //quitar espacios en blanco de nick
$_POST["pass"] = trim($_POST["pass"]); //quitar espacios de pass de pass

$result=mysqli_query($conexion,"select nick,pass from usuario where nick like '".$nick."'");

if ($row["pass"]==md5($pass)){
    $SESION["nick"]==$row['nick'];
    echo "usted ha conseguido acceder";
    header ("Location: http://localhost/proyecto/index.htm");
}
else{
   
    echo "contraseña incorrenta";
    header ("Location: http://localhost/proyecto/registration.htm");
    }
   
?>


CODIGO HTML

<!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" xml:lang="en" lang="en">

<head>
   <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
   <meta name="author" content="Jesus" />
<link href="./estilopor.css" rel="stylesheet" type="text/css" />


</head>

<body>

<table class="tablelogin">

<tr>
<td class="tduno">
<form action="./accesologin.php" method="POST">
user: <input type="text" name="nick"/><br />
pass: <input type="text" name="pass"/><br />
<input type="submit" value="enviar" name="enviar" />
</form>
</td>
</tr>
</table>

</body>
</html>


INCLUDE DE DONDE ESTA LA CONEXION A LA BASE DE DATOS

<?

include_once('bd.php');

function conectabd(){
$conexion = mysqli_connect("localhost","root","1234","proyecto");

if (mysqli_connect_errno()){ //error !=0 igual a fallo
printf("Fallo en la conexion a la Base de datos: " .mysqli_connect_error());
exit();
}

mysqli_set_charset($conexion,"utf8");
return $conexion;

}


?>



LA BASE DE DATOS:

-- MySQL dump 10.13  Distrib 5.5.9, for Win32 (x86)
--
-- Host: localhost    Database: proyecto
-- ------------------------------------------------------
-- Server version   5.5.12

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- Current Database: `proyecto`
--

CREATE DATABASE /*!32312 IF NOT EXISTS*/ `proyecto` /*!40100 DEFAULT CHARACTER SET latin1 */;

USE `proyecto`;

--
-- Table structure for table `anuncio`
--

DROP TABLE IF EXISTS `anuncio`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `anuncio` (
  `cod` int(11) NOT NULL DEFAULT '0',
  `titulo` varchar(35) NOT NULL,
  `description` text NOT NULL,
  PRIMARY KEY (`cod`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `anuncio`
--

LOCK TABLES `anuncio` WRITE;
/*!40000 ALTER TABLE `anuncio` DISABLE KEYS */;
/*!40000 ALTER TABLE `anuncio` ENABLE KEYS */;
UNLOCK TABLES;

--
-- Table structure for table `producto`
--

DROP TABLE IF EXISTS `producto`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `producto` (
  `cod` int(11) NOT NULL DEFAULT '0',
  `description` text NOT NULL,
  `price` float DEFAULT NULL,
  PRIMARY KEY (`cod`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `producto`
--

LOCK TABLES `producto` WRITE;
/*!40000 ALTER TABLE `producto` DISABLE KEYS */;
/*!40000 ALTER TABLE `producto` ENABLE KEYS */;
UNLOCK TABLES;

--
-- Table structure for table `uap`
--

DROP TABLE IF EXISTS `uap`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `uap` (
  `nick` varchar(30) NOT NULL DEFAULT '',
  `cod` int(11) NOT NULL DEFAULT '0',
  `cod_producto` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`nick`,`cod`,`cod_producto`),
  KEY `cod` (`cod`),
  KEY `cod_producto` (`cod_producto`),
  CONSTRAINT `uap_ibfk_1` FOREIGN KEY (`nick`) REFERENCES `usuario` (`nick`),
  CONSTRAINT `uap_ibfk_2` FOREIGN KEY (`cod`) REFERENCES `anuncio` (`cod`),
  CONSTRAINT `uap_ibfk_3` FOREIGN KEY (`cod_producto`) REFERENCES `producto` (`cod`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `uap`
--

LOCK TABLES `uap` WRITE;
/*!40000 ALTER TABLE `uap` DISABLE KEYS */;
/*!40000 ALTER TABLE `uap` ENABLE KEYS */;
UNLOCK TABLES;

--
-- Table structure for table `usuario`
--

DROP TABLE IF EXISTS `usuario`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `usuario` (
  `nick` varchar(30) NOT NULL,
  `namee` varchar(30) NOT NULL,
  `last_name` varchar(30) NOT NULL,
  `pass` varchar(100) NOT NULL,
  `edad` int(11) NOT NULL,
  `email` varchar(30) DEFAULT NULL,
  `phone` int(11) NOT NULL,
  PRIMARY KEY (`nick`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `usuario`
--

LOCK TABLES `usuario` WRITE;
/*!40000 ALTER TABLE `usuario` DISABLE KEYS */;
INSERT INTO `usuario` VALUES ('maria','maria','maria','maria',21,'maria',675),('luis','luis','luis','1234',1234,'luis@gmail.com',45),('mm','mm','mm','mm',22,'mm',22),('pepe','pepe','pepe','pepe',33,'pep',33);
/*!40000 ALTER TABLE `usuario` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on 2011-05-26  3:11:18




Espero que puedan ayudarme..Muchas Gracias por su ayuda..

Un saludo.
Wenass,

en principio, cuando recoges del $_POST el usuario y password, mejor ponle esto por seguridad:
$nick = addslashes(trim($_POST["nick"])); //recoge datos del formulario
$pass = addslashes(trim($_POST["pass"])); //recooge datos del formulario


El md5() es para crear una "huella", no para encriptar. Creo que es mejor utilizar
SET @password:='password';

aes_encrypt($pass,@password); // Para introducirlo en la BBDD
aes_decrypt($pass,@password); // Para sacarlo de la BBDD


En teoría te envía siempre a http://localhost/proyecto/registration.htm porque no coincide if($row["pass"] == md5($pass))

Eso si, ten en cuenta que la longitud del valor que se inserta en la BBDD encriptado es mayor a la longitud del valor sin encriptar. (Aunque con el VARCHAR(100) creo que tendrás suficiente)

Prueba y ya nos contarás! [bye]
Además de lo que ya te comentaron, tienes un pequeño error en el nombre de la sesión que te va a afectar después cuando tengas que comprobar si ya está lgoeado:

$SESION["nick"]==$row['nick'];

tienes que cambiarlo por:
$_SESSION["nick"]==$row['nick'];
No entiendo varias cosas de tu codigo, por ejemplo
$_POST["nick"] = trim($_POST["nick"]); //quitar espacios en blanco de nick
$_POST["pass"] = trim($_POST["pass"]); //quitar espacios de pass de pass


Quitas los espacios y los guardas en el $_POST? No es muy logico, si eso tendrias que guardarlo en las variables anteriores que has creado.


Recuerda que las consultas a base de datos es importante poner las partes correspondientes en mayusculas
$result=mysqli_query($conexion,"[b]SELECT [/b]nick,pass [b]FROM [/b]usuario [b]WHERE[/b] nick [b]LIKE [/b] '".$nick."'");

Tambien puedes usar algo asi como
$result=mysqli_query($conexion,"[b]SELECT [/b]nick,pass [b]FROM [/b]usuario [b]WHERE[/b] nick= '".$nick."'");


Sobre el if, si vas a usar el row, es aconsejable $row=mysql_fetch_row ($result), antes de ponerlo en el if.

Como dice el compañero el $SESION["nick"]==$row['nick']; es $_SESION["nick"]==$row['nick'];
Buenas,

Siento el retraso en contestar es que estado con algunos asuntillos que han requerido todo mi tiempo.

Gracias a todos por los cambios y revision de PHP, ya e conseguido hacerlo funcionar..

Muchisimas Gracias a Todos..

Saludos.
4 respuestas