n4te02
08-03-2009, 10:11 AM
Hello all.. I have a site with a login system and everything, works fine. Now I am trying to add jquery ajax functionality to it.
I am trying to make it so the page doesn't change, so you click login, it displays a ajax-loader.gif or something then logs you in right then and there.
Here is the PHP for the login form:
<?php
if($_SESSION['in'] != "yes") // generate login form
{
$login = '<form action="login.php" id="" method="post">
<p>
<label class="loginField">Username:</label>
<input class="login" type="text" name="log" id="log" value="" />
</p>
<p>
<label class="loginField">Password:</label>
<input class="login" type="password" name="pwd" id="pwd" value="" />
</p>
<div style="text-align: right;">
<input type="submit" name="submitlogin" value="Submit" class="button_login" id="submit" />
</div>
<input type="hidden" name="redirect_to" value=""/>
</form><br /><a href="#submit_register" name="modal"><strong>Sign up</strong>
</a> | <a href="forgot.php">Forgot Password?</a>';
echo $login;
}
else
{
$login = 'Welcome back, <strong>'. $_SESSION['username'] .'</strong>. <a href="logout.php">Logout</a><br /><br /><br />
<a href="settings.php">Your Settings</a> | <a href="profile.php?user='. $_SESSION['username'] .'">View Profile</a> | <a href="upload.php">Upload Avatar</a>';
echo $login;
}
?>
(functions.php is loaded through in my header, that's what the db_connect(); is that you'll see)
and heres the login.php:
<?php
db_connect();
$username = $_POST['log'];
$password = $_POST['pwd'];
if (empty($username))
{
$errors .= 'Please enter a username.<br />';
}
if (empty($password))
{
$errors .= 'Please enter a password.<br />';
}
$check_login = mysql_fetch_array(mysql_query("SELECT * FROM `users` WHERE `username` = '$username'"));
if ($check_login['password'] != md5(md5(sha1($password))))
{
$errors .= 'The username/password combination you supplied is incorrect.<br />';
}
elseif(!$errors)
{
$_SESSION['user'] = $check_login;
$_SESSION['in'] = "yes";
$_SESSION['username'] = $check_login['username'];
}
if($errors) $maincontent = $errors;
else $maincontent = "<strong>Login successful.</strong> Return to <a href=\"index.php\">home</a>.
<script type=\"text/javascript\"><!--
setTimeout('Redirect()',1000);
function Redirect()
{
location.href = 'index.php';
}
// --></script>
";
echo $maincontent;
?>
I am fairly new to jquery, so any help is appreciated :)
I am trying to make it so the page doesn't change, so you click login, it displays a ajax-loader.gif or something then logs you in right then and there.
Here is the PHP for the login form:
<?php
if($_SESSION['in'] != "yes") // generate login form
{
$login = '<form action="login.php" id="" method="post">
<p>
<label class="loginField">Username:</label>
<input class="login" type="text" name="log" id="log" value="" />
</p>
<p>
<label class="loginField">Password:</label>
<input class="login" type="password" name="pwd" id="pwd" value="" />
</p>
<div style="text-align: right;">
<input type="submit" name="submitlogin" value="Submit" class="button_login" id="submit" />
</div>
<input type="hidden" name="redirect_to" value=""/>
</form><br /><a href="#submit_register" name="modal"><strong>Sign up</strong>
</a> | <a href="forgot.php">Forgot Password?</a>';
echo $login;
}
else
{
$login = 'Welcome back, <strong>'. $_SESSION['username'] .'</strong>. <a href="logout.php">Logout</a><br /><br /><br />
<a href="settings.php">Your Settings</a> | <a href="profile.php?user='. $_SESSION['username'] .'">View Profile</a> | <a href="upload.php">Upload Avatar</a>';
echo $login;
}
?>
(functions.php is loaded through in my header, that's what the db_connect(); is that you'll see)
and heres the login.php:
<?php
db_connect();
$username = $_POST['log'];
$password = $_POST['pwd'];
if (empty($username))
{
$errors .= 'Please enter a username.<br />';
}
if (empty($password))
{
$errors .= 'Please enter a password.<br />';
}
$check_login = mysql_fetch_array(mysql_query("SELECT * FROM `users` WHERE `username` = '$username'"));
if ($check_login['password'] != md5(md5(sha1($password))))
{
$errors .= 'The username/password combination you supplied is incorrect.<br />';
}
elseif(!$errors)
{
$_SESSION['user'] = $check_login;
$_SESSION['in'] = "yes";
$_SESSION['username'] = $check_login['username'];
}
if($errors) $maincontent = $errors;
else $maincontent = "<strong>Login successful.</strong> Return to <a href=\"index.php\">home</a>.
<script type=\"text/javascript\"><!--
setTimeout('Redirect()',1000);
function Redirect()
{
location.href = 'index.php';
}
// --></script>
";
echo $maincontent;
?>
I am fairly new to jquery, so any help is appreciated :)