log in page with member levels

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mallen
Forum Newbie
Posts: 9
Joined: Thu Sep 20, 2007 9:23 pm

log in page with member levels

Post by mallen »

I am creating a simple log in form to protect a "member" page. What i have done is assigned a "1" or a "2" to each subscriber. Only people with "2" can view the member page. Here is what I have so far. Its rough I know. I have some pseudo code in there just to get the outline right.

There are three pages. The member page that I am protecting, the login page that takes the submitted data, and the register page they get directed to if the log in fails.
----------------------------------------------------
member page
----------------------------------------------------

Code: Select all

<?php 
session_start();
$status = $status[0]; //member or not. If status "1" direct to register.php, if "2" show page.
session_register ('email');
session_register ('password');
session_register ('status');
 
if (isset ($POST['submit'])) {
if ((!empty ($_POST['email']))
 
if email and user name are a match when sent to login.php  
//show content on page below//
//All content will go here.....///
 
else 
//print form to sign in
print '<p>Please sign in using your email address and your password</p>';
 
print '<form action=login.php" method="post"><p>Email: <input type="text" name="email" size="20" /><br/>
Password: <input type="password" name="password" size="20" /><br /> <input type="submit" name="submit" value=Log in /></p> </form>';
?>
 
---------------------------------------------
The login.php page
---------------------------------------------

Code: Select all

<?php $username=$_POST['email'];//get data posted from members form
$password=$_POST['password'];
$success = "members.php";
$failed = "register.php";
//check user's email and password. Also if status is "1" redirect to register.php
//if status is "2" redirect to members.php and show the page.
SELECT email, password, and status FROM members
if email and password are ok and status is "2" then go to members page. Show as logged in.
else
if status is "1" redirect to register page.
User avatar
thomas777neo
Forum Contributor
Posts: 214
Joined: Mon Mar 10, 2003 6:12 am
Location: Johannesburg,South Africa

Re: log in page with member levels

Post by thomas777neo »

So what are you asking for exactly?
mallen
Forum Newbie
Posts: 9
Joined: Thu Sep 20, 2007 9:23 pm

Re: log in page with member levels

Post by mallen »

If I have the outline correct. I know I have pieces of code missing and I put pseudo code in to show what I am trying to do. Would like to know if thsi is the best approach.
mallen
Forum Newbie
Posts: 9
Joined: Thu Sep 20, 2007 9:23 pm

Re: log in page with member levels

Post by mallen »

I think I will add the status level later. For now i found some code and modified it. Does anything look incorrect here? Just checking email and password. Gives me message "Sorry, could not log you in. Wrong login information."

Code: Select all

 
<?php
session_start();
require_once('./Connections/'); ?>
<!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 Document</title>
</head>
 
<body>
<?php
 if ($_GET["op"] == "login")
 {
 if (!$_POST["email"] || !$_POST["password"])
  {
  die("You need to provide a username and password.");
  }
 
 // Create query
 $q = "SELECT * FROM `members` "
 ."WHERE `email`='".$_POST["email"]."' "
  ."AND `password`= PASSWORD('".$_POST["password"]."') "
  ."LIMIT 1";
 // Run query
 $r = mysql_query($q);
 
 if ( $obj = @mysql_fetch_object($r) )
  {
  // Login good, create session variables
  $_SESSION["valid_id"] = $obj->id;
  $_SESSION["valid_user"] = $_POST["email"];
  $_SESSION["valid_time"] = time();
 
  // Redirect to member page
  Header("Location: members.php");
  }
 else
  {
  // Login not successful
  die("Sorry, could not log you in. Wrong login information.");
 
  }
 }
else
 {
//If all went right the Web form appears and users can log in
 echo "<form action=\"?op=login\" method=\"POST\">";
 echo "Username: <input name=\"email\"><br />";
 echo "Password: <input name=\"password\"><br />";
 echo "<input type=\"submit\" value=\"Login\">";
 echo "</form>";
 }
?>
 
 
ashebrian
Forum Contributor
Posts: 103
Joined: Sat Feb 02, 2008 8:01 pm

Re: log in page with member levels

Post by ashebrian »

I had noticed that you do not have an error display section (i mean....once u click submit and it doesn't do what its supposed to do then there possibly an error) involved in the code.

use this and see if there's anything in ur output....if not the problem lies elsewhere.

Code: Select all

<?php error_reporting(E_ALL); ini_set('display_errors', true); ?>


It will make ur life a 100 times much easier
mallen
Forum Newbie
Posts: 9
Joined: Thu Sep 20, 2007 9:23 pm

Re: log in page with member levels

Post by mallen »

I get this error BEFORE I submit.
Notice: Undefined index: op in C:\login.php on line 4

But after there is no error.
ashebrian
Forum Contributor
Posts: 103
Joined: Sat Feb 02, 2008 8:01 pm

Re: log in page with member levels

Post by ashebrian »

Code: Select all

 if ($_GET["op"] == "login")
 {
 if (!$_POST["email"] || !$_POST["password"])1
try using this instead:

Code: Select all

if (isset($_GET['op']) && $_GET['op'] = 'login')
Post Reply