Jump to content

Recommended Posts

I have posted this topic several times in the past and I have never fully been able to receive the explanation I need, so I am hoping that someone will be willing to help me this time.  I am the webmaster for a local Boy Scout Troop and I am working on developing a PHP login system to provide a more personalized experience.  I am using MySQL for the user database and PHP to interface with it.  Currently I am able to add users via a webpage and authenticate the created user via a log in page.  I enter the user name and password on the page and it then displays a white page that says "Login Successful" if it is correct or redirects me back to the login page if it is incorrect.  I am very new to both MySQL and PHP and I  have no idea what to do after the user is successfully authenticated.  How would I go about using the currently logged in user to display personalized information on a subsection of my site and how would I change the contents of the pages if another user logged in? I am hoping to get a step to step guide on this if possible because I was told that I would have to use sessions, but I really don't understand how that works.  Below is included my HTML and PHP code for reference.

 

login.php (user authentication)

 

<?

$username="xxxx";

$password="xxxx";

$database="xxxx";

$host="xxxx";

 

$user=$_POST['user'];

$pass=$_POST['pass'];

 

@mysql_connect($host,$username,$password);

@mysql_select_db($database) or die("Unable to select database");

 

$sql="SELECT username, `password` FROM users WHERE username='$user' and `password`='$pass'";

$r=mysql_query($sql);

if(!$r){

$err=mysql_error();

print $err;

exit();

}

if(mysql_affected_rows()==0){

header("Location:user_login.htm");

print "Invalid User Name/Password. Please try again";

exit();

}

else{

header("Location:memberhome.htm");

exit();

}

?>

 

user_login.html (user login)

 

<!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">

 

<head>

<meta http-equiv="Content-Language" content="en-us" />

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Troop 322 of DC: Login</title>

<style type="text/css">

.style1 {

text-align: center;

}

.style30 {

vertical-align: middle;

}

.style31 {

font-family: Arial;

}

.style32 {

margin-left: 5px;

}

.style33 {

font-family: "Arial Black";

font-size: medium;

}

</style>

</head>

 

<body style="background-color: #9E9A61">

<div class="style1">

<table style="width: 100%">

<tr>

<td>

<img alt="American Flag" src="American%20Flag.gif" width="161" height="90" /></td>

<td>

<img alt="Crest" src="Honor%20Crest.gif" width="125" height="122" class="style30" /></td>

<td>

<img alt="PA Flag" src="PA%20Flag.gif" width="152" height="96" /></td>

</tr>

<tr>

<td colspan="3">

<img alt="Banner" src="Login%20Banner.jpg" width="650" height="120" /></td>

</tr>

</table>

<br /><span class="style33">Enter the User Name and Password assigned to you

below to access personalized pages and information on the site.  If you

do not have an account, please contact one of our Webmasters and a profile

will be set up for you.<br />

</span><br />

</div>

<form method="post" action="login.php">

<div class="style1">

<span class="style31"><strong>User Name    

<input name="user" type="text" style="width: 150px" /><br />

 Password    </strong></span><strong>

<input name="pass" type="password" style="width: 153px" class="style32" /><br />

</strong>

<br />

<input name="submit" type="submit" value="Log In" /></div>

</form>

 

</body>

 

</html>

 

 

register.php (user registration)

 

<?

$username="xxxx";

$password="xxxx";

$database="xxxx";

$host="xxxx";

 

$user=$_POST['user'];

$pass=$_POST['pass'];

$firstname=$_POST ['firstname'];

$lastname=$_POST ['lastname'];

$email=$_POST ['email'];

 

@mysql_connect($host,$username,$password);

@mysql_select_db($database) or die("Unable to select database");

 

$query = "INSERT into users VALUES ('','$user','$pass','$firstname','$lastname','$email')";

mysql_query($query);

 

mysql_close();

?>

 

user_registration.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">

 

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Untitled 1</title>

</head>

 

<body>

<form action="register.php" method="post">

Desired User Name    <input type="text" name="user">

<br />

Desired Password     <input type="text" name="pass">

<br />

<br />

First Name                <input type="text" name="firstname" size="30">

<br />

Last Name               

<input type="text" name="lastname"

<br style="width: 187px" />

<br />

Email                       

<input type="text" name="email"

 

<br /> <br />

<br />

<input type="submit" value="Register" name="submit">

 

</form>

</body>

</html>

 

Thank you in advance for your assistance!

 

 

 

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/208251-php-login-system-please-help/
Share on other sites

<?php
session_start();

$username="xxxx";
$password="xxxx";
$database="xxxx";
$host="xxxx";

$user=stripslashes(mysql_real_escape_string($_POST['user']));
$pass=stripslashes(mysql_real_escape_string($_POST['pass']));

@mysql_connect($host,$username,$password);
@mysql_select_db($database) or die("Unable to select database");

$sql="SELECT username, `password` FROM users WHERE username='$user' and `password`='$pass'";
$r=mysql_query($sql);
if(!$r){
$err=mysql_error();
print $err;
exit();
}
if(mysql_affected_rows()==0){
header("Location:user_login.htm");
print "Invalid User Name/Password. Please try again";
exit();
}
else{
header("Location:memberhome.htm");
$_SESSION["user"] = $user;
exit();
}
?>

 

memberhome.php <- note it must be php

<?php
session_start();
if(!$_SESSION["user"]){
header("location: index.php");
}
// all the code that the member can see
echo "Welcome back," . $_SESSION["user"];
?>

 

I suggest you look into this tutorial - http://phpeasystep.com/phptu/6.html

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.