[PHP] Login Page.

Status
Not open for further replies.
Joined
Oct 28, 2008
Messages
1,557
Reaction score
369
Well hey there, I've been coding an user system, but I've got an error, when I login it doesn't redirect to my /me.php (profile sorta page). Well it just redirect's me back to the /index.php page? :?:.Here's my login code;

EDIT: It redirect's me to /index.php but when I type it in manually it has logged me in.

PHP:
<?
// * Login page
// * created by powahCoder, aka PowahAlert
include("include/session.php");
?>

<html>
<title>Uhh</title>
<body>

<table>
<tr><td>


<?php
if($logged_in){

header('Location:http://www.mysite.com/me.php');


  

}
else{
?>

<h1>Login</h1>
<?
/**
 * User not logged in, display the login form.
 * If user has already tried to login, but errors were
 * found, display the total number of errors.
 * If errors occurred, they will be displayed.
 */
if($form->num_errors > 0){
   echo "<font size=\"2\" color=\"#ff0000\">".$form->num_errors." error(s) found</font>";
}
?>
<form action="process.php" method="POST">
<table align="left" border="0" cellspacing="0" cellpadding="3">
<tr><td>Username:</td><td><input type="text" name="user" maxlength="30" value="<? echo $form->value("user"); ?>"></td><td><? echo $form->error("user"); ?></td></tr>
<tr><td>Password:</td><td><input type="password" name="pass" maxlength="30" value="<? echo $form->value("pass"); ?>"></td><td><? echo $form->error("pass"); ?></td></tr>
<tr><td colspan="2" align="left"><input type="checkbox" name="remember" <? if($form->value("remember") != ""){ echo "checked"; } ?>>
<font size="2">Remember me next time     
<input type="hidden" name="sublogin" value="1">
<input type="submit" value="Login"></td></tr>
<tr><td colspan="2" align="left"><br><font size="2">[<a href="forgotpass.php">Forgot Password?</a>]</font></td><td align="right"></td></tr>
<tr><td colspan="2" align="left"><br>Not registered? <a href="register.php">Sign-Up!</a></td></tr>
</table>
</form>

<?
}

/**
 * Just a little page footer, tells how many registered members
 * there are, how many users currently logged in and viewing site,
 * and how many guests viewing site. Active users are displayed,
 * with link to their user information.
 */
echo "</td></tr><tr><td align=\"center\"><br><br>";
echo "<b>Member Total:</b> ".$database->getNumMembers()."<br>";
echo "There are $database->num_active_users registered members and ";
echo "$database->num_active_guests guests viewing the site.<br><br>";

include("include/view_active.php");

?>


</td></tr>
</table>


</body>
</html>

Much love,
Livar.
 
Last edited:
Well, if me.php redirects you back to index.php if you're not logged in, then that could be why.

I don't see type of cookies or sessions, so how else would the actual me.php know that you're logged in?

I suggest, where the form is successful, to put
$username <- username of the user that logged in
$_SESSION['user'] = $username;

Then put
PHP:
session_start();
at the top of your page that way you can then do:
PHP:
<?PHP
if($_SESSION['user']){//The user is logged in
  //redirect..
}else{
  //form
}
?>

And for authentication purposes in your me.php do the same thing...
PHP:
<?PHP
session_start();
if($_SESSION['user']}{
  //Display member area stuff
}else{
  //redirect to index..
}
?>


That's how you could potentially do it, I guess.
PHP: Sessions - Manual
 
I CBA to go look through my code.. But ill explain how i did it..

User sends username and password through form on index.php to login.php

Login.php compares $_POST["username"] and $_POST["userpass"] to $name and $pass.. if they are incorrect then it redirects to index.php if its correct then it changes $_SESSION["user"] to $name.

now. In index.php it checks if $_SESSION["user"] has a value. if it does then it auto redirects to me.php if not then it continues loading index.php

i hope i helped.

King Towel
 
Last edited:
EDIT: My new code;
PHP:
<?
// * Login page
// * created by powahCoder, aka PowahAlert
include("include/session.php");
?>
<html>
<title>Uhh</title>
<body>

<table>
<tr><td>






  
<?
/**
 * User has already logged in, so display relevant links, including
 * a link to the admin center if the user is an administrator.
 */
if($session->logged_in){
   echo "<h1>Logged In</h1>";
   echo "Welcome <b>$session->username</b>, you are logged in. <br><br>"
       ."[<a href=\"userinfo.php?user=$session->username\">My Account</a>]   "
       ."[<a href=\"useredit.php\">Edit Account</a>]   ";
   if($session->isAdmin()){
      echo "[<a href=\"admin/admin.php\">Admin Center</a>]   ";

   }
   echo "[<a href=\"process.php\">Logout</a>]";
}
else{
?>

<h1>Login</h1>
<?
/**
 * User not logged in, display the login form.
 * If user has already tried to login, but errors were
 * found, display the total number of errors.
 * If errors occurred, they will be displayed.
 */
if($form->num_errors > 0){
   echo "<font size=\"2\" color=\"#ff0000\">".$form->num_errors." error(s) found</font>";
}
?>
<form action="process.php" method="POST">
<table align="left" border="0" cellspacing="0" cellpadding="3">
<tr><td>Username:</td><td><input type="text" name="user" maxlength="30" value="<? echo $form->value("user"); ?>"></td><td><? echo $form->error("user"); ?></td></tr>
<tr><td>Password:</td><td><input type="password" name="pass" maxlength="30" value="<? echo $form->value("pass"); ?>"></td><td><? echo $form->error("pass"); ?></td></tr>
<tr><td colspan="2" align="left"><input type="checkbox" name="remember" <? if($form->value("remember") != ""){ echo "checked"; } ?>>
<font size="2">Remember me next time     
<input type="hidden" name="sublogin" value="1">
<input type="submit" value="Login"></td></tr>
<tr><td colspan="2" align="left"><br><font size="2">[<a href="forgotpass.php">Forgot Password?</a>]</font></td><td align="right"></td></tr>
<tr><td colspan="2" align="left"><br>Not registered? <a href="register.php">Sign-Up!</a></td></tr>
</table>
</form>

But at this code;

PHP:
<?
/**
 * User has already logged in, so display relevant links, including
 * a link to the admin center if the user is an administrator.
 */
if($session->logged_in){
   echo "<h1>Logged In</h1>";
   echo "Welcome <b>$session->username</b>, you are logged in. <br><br>"
       ."[<a href=\"userinfo.php?user=$session->username\">My Account</a>]   "
       ."[<a href=\"useredit.php\">Edit Account</a>]   ";
   if($session->isAdmin()){
      echo "[<a href=\"admin/admin.php\">Admin Center</a>]   ";

   }
   echo "[<a href=\"process.php\">Logout</a>]";
}
else{
?>
I try to add this:
PHP:
<?
/**
 * User has already logged in, so display relevant links, including
 * a link to the admin center if the user is an administrator.
 */
if($session->logged_in){
header( 'Location: http://wwww.mysite.com/me.php' ) ;   
else{
?>
It gives me an 500 HTTP error?
 
Last edited:
EDIT: My new code;
PHP:
<?
// * Login page
// * created by powahCoder, aka PowahAlert
include("include/session.php");
?>
<html>
<title>Uhh</title>
<body>

<table>
<tr><td>






  
<?
/**
 * User has already logged in, so display relevant links, including
 * a link to the admin center if the user is an administrator.
 */
if($session->logged_in){
   echo "<h1>Logged In</h1>";
   echo "Welcome <b>$session->username</b>, you are logged in. <br><br>"
       ."[<a href=\"userinfo.php?user=$session->username\">My Account</a>]   "
       ."[<a href=\"useredit.php\">Edit Account</a>]   ";
   if($session->isAdmin()){
      echo "[<a href=\"admin/admin.php\">Admin Center</a>]   ";

   }
   echo "[<a href=\"process.php\">Logout</a>]";
}
else{
?>

<h1>Login</h1>
<?
/**
 * User not logged in, display the login form.
 * If user has already tried to login, but errors were
 * found, display the total number of errors.
 * If errors occurred, they will be displayed.
 */
if($form->num_errors > 0){
   echo "<font size=\"2\" color=\"#ff0000\">".$form->num_errors." error(s) found</font>";
}
?>
<form action="process.php" method="POST">
<table align="left" border="0" cellspacing="0" cellpadding="3">
<tr><td>Username:</td><td><input type="text" name="user" maxlength="30" value="<? echo $form->value("user"); ?>"></td><td><? echo $form->error("user"); ?></td></tr>
<tr><td>Password:</td><td><input type="password" name="pass" maxlength="30" value="<? echo $form->value("pass"); ?>"></td><td><? echo $form->error("pass"); ?></td></tr>
<tr><td colspan="2" align="left"><input type="checkbox" name="remember" <? if($form->value("remember") != ""){ echo "checked"; } ?>>
<font size="2">Remember me next time     
<input type="hidden" name="sublogin" value="1">
<input type="submit" value="Login"></td></tr>
<tr><td colspan="2" align="left"><br><font size="2">[<a href="forgotpass.php">Forgot Password?</a>]</font></td><td align="right"></td></tr>
<tr><td colspan="2" align="left"><br>Not registered? <a href="register.php">Sign-Up!</a></td></tr>
</table>
</form>
<?PHP
}
?>

Make everything single quotes, and wrap it all in a fucking echo ""; All that other **** is sloppy, imo. But then again I don't go by the validation, etc. But I don't know, read up on the links foxx gave you.
 
Code:
Server error.

The website encountered an error while retrieving http://www.l-shekhani.co.uk/. It may be down for maintenance or configured incorrectly.

Here are some suggestions:
Reload this web page later.
  More information on this error
Below is the original error message

HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfill the request.

^ Code.
 
PHP:
public static function user ($username, $password) {
	if (preg_match("/^[0-9a-zA-Z]*$/i", $username) && preg_match ("/^[0-9a-z-A-Z]*$/i", $password)) {
		$username 	= stripslashes(mysql_real_escape_string($username));
		$password 	= stripslashes(mysql_real_escape_string(md5($password)));
		$result		= mysql_query("SELECT * FROM accounts WHERE username=\"$username\" and password=\"$password\"");
		$row 		= mysql_fetch_array($result);
		if(mysql_num_rows($result) == 1 ){
			return true;
		} else {
			return false;
		}
	} else {
		return false;
	}
}

PHP:
if (Action::user(Form::get("username"), Form::get("password")) == true) {
	Action::session("set", "username", Form::get("username"));
	Action::session("set", "password", Form::get("password"));
	header ("location: ?page=cpanel");
} else {
	header("location: ?page=login");
}

My function "user" retrieves username and password, doing so it checks if it contains only numbers and letters, if not it returns false, otherwise it goes through database check after being sanitized.

If the value returns true, a session is then set for username/password.

NOTE:
When dealing with users, you never want to assign sessions that refer to them being logged in, or anything personal. You will run into a lot of problems especially if you do that with cookies.

Security should be your main priority and if you're looking for a quick fix, store username/password as seperate sessions and anytime the data is needed for authorization do a check with the sessions.


Hopefully that works.
 
Status
Not open for further replies.
Back