Dumb question: making a login box

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dailydiapers
    Confirmed User
    • Oct 2003
    • 1080

    #1

    Dumb question: making a login box

    Okay, forgive the dumb noob question, but i'm a self-taught webmaster and just don't know some things ya'll think are simple

    I'm redesigning one of my sites that's a combination of free and pay services. On the fre side I want to put a login box for the protected directory, rather than using the pop-up login box you get when you link directly to the protected directory. Can someone tell me how to write (or give me a template) the code for the login box?
    Mike
    http://www.dailydiapers.com/
  • ssp
    Confirmed User
    • Jan 2005
    • 7990

    #2
    Use PHP and lookup an online tutorial on how to do this. There are loads and loads around which help you make one step by step.

    Comment

    • Fake Nick
      So Fucking Banned
      • Jul 2004
      • 7708

      #3
      http://www.gofuckyourself.com/showthread.php?t=437375

      Comment

      • Dailydiapers
        Confirmed User
        • Oct 2003
        • 1080

        #4
        PHP is one of those dumbfounded area for me, looking to include it (log-in box) in a straight HTML design.
        Mike
        http://www.dailydiapers.com/

        Comment

        • calmlikeabomb
          Confirmed User
          • May 2004
          • 1323

          #5
          Use php and sql

          Your login form:
          Code:
          <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method=p.ost>
          
          Username: <input type="text" name="username" maxlength="20" />
          Password:<input type="password" name="password" maxlength="20" />
          <input type="submit" name="submit" value="Login">
          
          </form>
          If this form is submitted check for user in database set cookies and redirect.
          Code:
          <?php
          
          // Create a function for escaping the data.
          	function escape_data ($data) {
          		global $dbc; // Need the connection.
          		if (ini_get('magic_quotes_gpc')) {
          			$data = stripslashes($data);
          		}
          		return mysql_real_escape_string($data, $dbc);
          } // End of function.
          
          if (isset($_POST['submit'])) { // Check if the form has been submitted.
          
          	require_once ('../mysql_connect.php'); // Connect to the database.
          	
          	if (empty($_POST['username'])) { // Validate the username.
          		$u = FALSE;
          		echo '<p><font color="red" >You forgot to enter your username!</font></p>';
          	} else {
          		$u = escape_data($_POST['username']);
          	}
          	
          	if (empty($_POST['password'])) { // Validate the password.
          		$p = FALSE;
          		echo '<p><font color="red" >You forgot to enter your password!</font></p>';
          	} else {
          		$p = escape_data($_POST['password']);
          	}
          	
          	if ($u && $p) { // If everything's OK.
          	
          		// Query the database.
          		$query = "SELECT customer_id, firstname FROM customers WHERE username='$u' AND password=PASSWORD('$p')";		
          		$result = @mysql_query ($query);
          		$row = mysql_fetch_array ($result, MYSQL_NUM); 
          		
          		if ($row) { // A match was made.
          				
          				// Start the session, register the values & redirect.
          				setcookie ('user_id', $row[0], time()+2419200, '/', '', 0);
          				header ("Location:  http://www.domain.com/index.php");
          				ob_end_flush(); // Delete the buffer.
          				exit();
          				
          		} else { // No match was made.
          			echo '<p><font color="red" >The username and password entered do not match those on file.</font></p>'; 
          		}
          		
          		mysql_close(); // Close the database connection.
          		
          	} else { // If everything wasn't OK.
          		echo '<p><font color="red" >Please try again.</font></p>';		
          	}
          	
          } // End of SUBMIT conditional.
          Protect pages with something like this:
          Code:
          <?php
          if (!isset($_COOKIE['user_id'])) {
          	header ("Location:  http://www.domain.com/login.php");
          	}
          ?>
          You'll also need a form to register your users to the database.


          Dumbass
          subarus.

          Comment

          • ssp
            Confirmed User
            • Jan 2005
            • 7990

            #6
            Originally posted by Dailydiapers
            PHP is one of those dumbfounded area for me, looking to include it (log-in box) in a straight HTML design.
            Creating a login box wont be difficult. But you need some kind of dynamics to process whats submitted. Either Perl, PHP, ASP.

            Comment

            • stevo
              Confirmed User
              • Aug 2002
              • 2051

              #7
              Can you have PHP read a .htpasswd file? Or does it have to read from its own database?

              Comment

              Working...