Page 1 of 1

How to make a very simple user authentication routine?

Posted: Sun Nov 01, 2009 1:06 am
by deliberatelies
I've been reading tutorials on user authentication but it's all overboard for what I need:

I have a web page with downloads I only want accessible to people subscribed to my email newsletter. I'd like a form where someone types in their email address, and have the code just check to see if there's a match in my already existing newsletter mysql database. If there's a match, they get access to the downloads page and preferably aren't prompted to type in their email address again for another couple of weeks.

Anyone know of any existing tutorials that spell out how to do this, and nothing more complex? I don't need to manage actual login sessions or deal with passwords or any of that crap. Security of the content is not a real concern; it's just an incentive for people to be on the mailing list.

Thanks in advance for anyone who feels like saving me time and headaches! Cheers.

Re: How to make a very simple user authentication routine?

Posted: Mon Nov 02, 2009 4:13 pm
by dickey
Hello deliberatelies (cool name),

I remember only too well, what a bother it was first time to digest writing authentication scripts.

I just wrote this based on the requirements of your post. It should be fairly easy to wrap your head around, and workable for your circumstances.

Code: Select all

 
<?php
  
  // used to identify and post to the current script
  $thisScript = $_SERVER['SCRIPT_NAME'];
 
  // if the value of emailAddress is not available to this form
  // show the email login form
  if (!$_POST['emailAddress'])
  {
    are_you_on_my_mailing_list($thisScript);  
  }
  else  // your user has submitted the email login form
  {
    // obtain the email address
    $emailAddress = $_POST['emailAddress'];
    // check whether it is a valid email
    
    //connect to db
    $link = mysql_connect("yourserver", "youruser", "yourmysqluserpassword") or die("server connection error");
    mysql_select_db("yourmysqldatabase") or die("database connection error");
    
    $q = "SELECT userName FROM myUserInfoTable WHERE emailAddress = '$emailAddress'"; // your query to change whether your user's email is already on file'
    $r = mysql_query($q);  // run query
    
    if (mysql_affected_rows($link) ==1)// if the last mysql query has 1 row - your user is authenticated
    {
      $ar = mysql_fetch_assoc($r);  // get result in associative array
      $userName = $ar['userName'];  // get the value of userName from your result set to be used for a personal greeting
      
      // show content
      
      echo 'Hello '.$userName.', Welcome back to http://www.yoursite.com<br /><br />';
      echo 'show your newsletter or content here.<br />';  
      echo 'show your newsletter or content here.<br />';  
      echo 'show your newsletter or content here.<br />';  
      echo 'show your newsletter or content here.<br />';  
    }
    else  // your user was not authenticated - ask then to try again or subscribe to your site
    {
       // ask them to sign in again
       echo 'Your email address was not on file. Please try again<br /><br />'; 
       are_you_on_my_mailing_list($thisScript);
       
       // or provide a link to your sign up page
       echo 'If you are new to http://www.yoursite.com, sign up free for our exclusive newsletter.<br/>';
       echo '<a href="www.yoursite.com/signup.php">sign up now</a>';
    }
   
    
    mysql_close($link); //closes db connect
  }
?>
 
 
<?php
  function are_you_on_my_mailing_list($thisScript)
  {
?>      
  <form action="<?php echo $thisScript; ?>" method="post">
  <table>
    <tr>
      <td>enter your email address</td>
      <td><input type="text" name="emailAddress"></td>
    </tr>
    <tr>
      <td><input type="submit" value="validate me"></td>
    </tr>
  </table>
  </form>      
<?php      
  }
?>
 
The basic idea is to challenge all user to the page for their email address, and once you have it you show them the page. If their email is not on file (in your db), then challenge them for it again, or offer to sign them up to your site. Too easy when you know how, a nightmare when you don't.

Once you have it working you can then dress up your page as usual.

Have a great day.

Kind regards, Andrew

Let me know how you go.

Re: How to make a very simple user authentication routine?

Posted: Tue Nov 03, 2009 1:09 pm
by deliberatelies
Oh wow, I wish I had noticed this earlier! I ended up spending an incredible amount of time learning how to work with mysql with php - haha, never a bad thing I suppose. Thanks so much for your reply, it gave me another angle to look at for a solution.