I have spent a lot of time recently writing javascript, and I recently discovered jQuery. It is pretty much amazing. Here's a super easy thing I wrote to use jQuery's ajax stuff to log in a user without refreshing the page:
1). login.js
function logMein() {
var username = $("#usernameId").val();
var password = $("#passwordId").val();
$.post("./login.php", { username: username, password: password }, function(welcome) { $("#loginDiv").html(welcome); } );
}
2). element to get jQuery:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
3). put <div id="loginDiv"><?php require_once('login.php'); ?></div> where you want to have the login thingy
4). login.php
@session_start();
require_once('class/user.class.php');
// checks to see if user submitted form stuff, if yes, tries to login
if (isset($_POST['username']) && strlen($_POST['password']) >= 6) {
$username = &$_POST['username'];
$password = &$_POST['password'];
$userLogin = new User($username);
if ($userLogin->getExists() === TRUE && $userLogin->passwordMatch($password) === TRUE ) {
$_SESSION['username'] = $username;
}
}
// if user has been logged in successfully, say welcome, else rewrite form
if (isset($_SESSION['username'])) {
$userObj = new User($_SESSION['username']);
$userInfo['id'] = $userObj->getId();
$userInfo['username'] = $userObj->getUsername();
$userInfo['firstName'] = $userObj->getFirstName();
$userInfo['lastName'] = $userObj->getLastName();
$userInfo['email'] = $userObj->getEmail();
$userInfo['city'] = $userObj->getCity();
$userInfo['state'] = $userObj->getState();
$userInfo['country'] = $userObj->getCountry();
echo "Welcome ".$userInfo['firstName']."! ";
echo "Logout";
} else {
?>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<form name="login" action="" onsubmit="return false" method="post">
username:<input type="text" id="usernameId" name="username" maxlength="50" value="" />
password:<input type="password" id="passwordId" name="password" value="" />
<input type="submit" id="submitLogin" value="Login" onclick="logMein();" />
</form>
}
?>
Update June 2, 2009:
Per neniu's request, this should be the User class I wrote for this script. I do not think it has been thoroughly tested, as I never got around to implementing this in a production environment.
require_once("{$_SERVER['DOCUMENT_ROOT']}/db/mysql_connect.php");
// need to fix all the mysql_real_escape_strin()g stuff
class User {
private $db;
private $username;
private $id;
private $firstName;
private $lastName;
private $password;
private $email;
private $city;
private $state;
private $country;
private $exists;
function __construct($username=NULL) {
$this->db = db_connect();
if ($username != NULL) {
$username = mysqli_real_escape_string($username);
$query = "SELECT * FROM `user_tb` WHERE `username`='$username' LIMIT 1";
$result = $this->db->query($query);
if ($result->num_rows < 1) {
$this->exists = FALSE;
} else {
$this->exists = TRUE;
$row = $result->fetch_array();
$this->username = $row['username'];
$this->id = $row['id'];
$this->firstName = $row['firstName'];
$this->lastName = $row['lastName'];
$this->password = $row['password'];
$this->email = $row['email'];
$this->city = $row['city'];
$this->state = $row['state'];
$this->country = $row['country'];
}
}
}
function getUsername() {
return $this->username;
}
function getExists() {
return $this->exists;
}
function getId() {
return $this->id;
}
function getFirstName() {
return $this->firstName;
}
function getLastName() {
return $this->lastName;
}
function getEmail() {
return $this->email;
}
function getCity() {
return $this->city;
}
function getState() {
return $this->state;
}
function getCountry() {
return $this->country;
}
function passwordMatch($password) {
if ($this->password == sha1($password)) { return TRUE; }
else { return FALSE; }
}
function addUser($username, $firstName, $lastName, $password, $email, $city, $state, $country) {
$username = mysqli_real_escape_string($username);
$firstName = mysqli_real_escape_string($firstName);
$lastName = mysqli_real_escape_string($lastName);
$password = sha1($password);
$email = mysqli_real_escape_string($email);
$city = mysqli_real_escape_string($city);
$state = mysqli_real_escape_string($state);
$country = mysqli_real_escape_string($country);
$query = "INSERT INTO `user_tb` (`username`,`firstName`,`lastName`,`password`,`email`,`city`,`state`,`country`)
VALUES ('$username','$firstName','$lastName','$password','$email','$city','$state','$country')";
if (!$this->db->query($query)) {
echo "Failed to create user.\n
".$this->db->error; return FALSE;
} else { return TRUE; }
}
function editUser($username, $firstName, $lastName, $password, $email, $city, $state, $country) {
$username = mysqli_real_escape_string($username);
$firstName = mysqli_real_escape_string($firstName);
$lastName = mysqli_real_escape_string($lastName);
$password = sha1($password);
$email = mysqli_real_escape_string($email);
$city = mysqli_real_escape_string($city);
$state = mysqli_real_escape_string($state);
$country = mysqli_real_escape_string($country);
$query = "UPDATE `user_tb` SET
`username`='$username', `firstName`='$firstName', `lastName`='$lastName', `password`='$password', `email`='$email',
`city`='$city',`state`='$state',`country`='$country' WHERE `username`='$username' LIMIT 1";
if (!$this->db->query($query)) {
echo "Failed to edit user.\n
".$this->db->error; return FALSE;
} else { return TRUE; }
}
}
?>
Subscribe to:
Post Comments (Atom)
S.J. Fuhry's Favorite Books
- Aristotle, "Nicomachean Ethics"
- Augustine, St., "Confessiones"
- Barron, Fr. Robert, "Heaven in Stone and Glass"
- Barron, Fr. Robert, "The Strangest Way"
- Benedict XVI, "Deus Caritas Est"
- Chesterton, G.K., "Orthodoxy"
- Chesterton, G.K., "The Ballad of the White Horse"
- Chesterton, G.K., "The Dumb Ox"
- Chesterton, G.K., "The Everlasting Man"
- Chesterton, G.K., "The Well and the Shallows"
- John Paul II, "Fides et Ratio"
- John Paul II, "Theology of the Body"
- John Paul II, "Veritatis Splendor"
- Leo XIII, Pope, "Rerum Novarum"
- Lewis, C.S., "The Abolition of Man"
- O'Connor, Flannery, "A Good Man Is Hard to Find and Other Stories"
- Pearce, Joseph, "Literary Converts"
- Pearce, Joseph, "Tolkien: Man and Myth"
- Pearce, Joseph, "Wisdom and Innocence"
- Ratzinger, Joseph Cardinal, "The Ratzinger Report"
- Ratzinger, Joseph Cardinal, "The Spirit of the Liturgy"
- Shakespeare, "Hamlet"
- Shakespeare, "Henry V"
- Shakespeare, "The Tempest"
- Sokolowski, Robert, "Introduction to Phenomenology"
- Sokolowski, Robert, "The God of Faith and Reason"
- Tolstoy, Leo, "The Death of Ivan Ilyich"
- von Balthasar, Hans Urs, "Prayer"
- Waugh, Evelyn, "Brideshead Revisited"
- Wiegel, George, "Letters to a Young Catholic"
- Wojtyla, Karol (John Paul II), "Love and Responsibility"

3 comments:
boa, where is userclass?
not sure if it still works (not currently using this in a production environment), but I added whatever "User" class that was. Looks a little iffy, but it probably mostly works.
Verz nice and useful!
Post a Comment