XHTML:
Código HTML:
<?php session_start(); ?> <!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" xml:lang="es-ar" lang="es-ar" dir="ltr"> <head> <!-- CSS Documents --> <link rel="stylesheet" type="text/css" media="screen" href="css/reset.css" charset="utf-8" /> <link rel="stylesheet" type="text/css" media="screen" href="css/mainContent.css" charset="utf-8" /> <link rel="stylesheet" type="text/css" media="screen" href="css/jquery-ui-1.8.4.custom.css" charset="utf-8" /> <!-- Scripts --> <script type="text/javascript" src="scripts/js/jquery-1.4.4.min.js"></script> <script type="text/javascript" src="scripts/js/login.js"></script> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="content-language" content="es-ar" /> </head> <body> <?php if(!isset($_SESSION['username']) || $_SESSION['username'] == ' '){ ?> <!-- Login form --> <div id="loginForm"> <div id="errorMessage"></div> <form id="form" name="form" method="post" action=""> <fieldset> <dl> <dt><label for="username">Usuario:</label></dt> <dd><input id="username" class="inputFields" name="username" type="text" size="20" maxlength="20" tabindex="1" value="" /></dd> </dl> <dl> <dt><label for="password">Contraseña:</label></dt> <dd><input id="password" class="inputFields" name="password" type="password" size="20" maxlength="20" tabindex="2" value="" /></dd> </dl> <dl> <dd><input id="submit" name="submit" type="button" value="Ingresar" /></dd> </dl> </fieldset> </form> </div> <?php } ?> </body> </html>
Código:
session.php$(document).ready(function(){ $("#username").focus(); $("#submit").click(function(){ $.ajax({ url: "php/session.php", cache: false, type: "POST", data: "username=" + $("#username").val() + "&password=" + $("#password").val(), success: function(){ alert('success'); }, error: function(){ $("#form").effect("shake", {times:2}, 100); $("#errorMessage") .css('margin', '30px 0 10px 9px') .attr('class', 'ui-state-error') .html('<strong>ERROR - </strong>Los datos ingresados son incorrectos'); } }); }); });
Código PHP:
<?php
session_start();
if(!isset($_SESSION['username'])){
if(isset($_POST['username'])){
$db = new mysqli("localhost", "username", "password", "bd");
$stmt = $db->prepare("SELECT username FROM users WHERE username = ? AND password = ?");
$stmt->bind_param('ss', $_POST['username'], $_POST['password']);
$stmt->execute();
$stmt->store_result();
if($stmt->num_rows() == 1){
$stmt->bind_result($username);
$stmt->fetch();
$_SESSION['username'] = $username;
header("Location: http://www.google.com.ar/");
}
}
}
?>
Desde ya disculpen las molestias nuevamente y muchísimas gracias!
Suertee! =)