Password Protection & Redirect to User sepcific area
Posted: Mon Aug 23, 2010 5:29 pm
Hi Guys,
I'm totally new to php and I'm trying to code a password protection script that will will redirect the user to their specific page based on their username and password combo. If you guys have any ideas on the best way to do this please let me know. I was trying to reference a mysql database that would authenticate the user and then send them to their specific page. I have the authentication working but unfortunately not the redirect. My database basically looked like 4 fields, one with numercial auto_incremting two digit number, one with the username, one with the password, and one with the redirect the url. Below I have posted my code thus far.
database.php
loginscript.php
and then I have a login form on a generic html page
<form action="loginscript.php" method="post">
Username:
<input type="text" name="username" /><br />
Password:
<input type="password" name="password" /><br />
<input type="submit" value="Submit" />
</form>
What I can't figure out is this:
Once I authenticate the user I need to pull that users unique url from mysql database and then redirect them there. I know this should be simple enough but every time I try something either I get errors or nothing at all. I wanted to try to pull the url from the database, set it to a session variable, and use header to redirect to that variable. I just don't enough about php and its syntax to pull it off. Thanks in advance for your help guys.
I'm totally new to php and I'm trying to code a password protection script that will will redirect the user to their specific page based on their username and password combo. If you guys have any ideas on the best way to do this please let me know. I was trying to reference a mysql database that would authenticate the user and then send them to their specific page. I have the authentication working but unfortunately not the redirect. My database basically looked like 4 fields, one with numercial auto_incremting two digit number, one with the username, one with the password, and one with the redirect the url. Below I have posted my code thus far.
database.php
Code: Select all
<?php
$conn = mysql_connect("localhost","*user*","*password*");
$db = mysql_select_db("*database_name*");
Code: Select all
<?php
// Connect to the database
require('database.php');
// Set username and password variables for this script
$user = mysql_real_escape_string($_POST["username"]);
$pass = mysql_real_escape_string(md5($_POST["password"]));
// Make sure the username and password match, selecting all the client's
// data from the database if it does. Store the data into $clientdata
$clientdata = mysql_query("SELECT * FROM Clients WHERE username='$user' and password='$pass'")
or die (mysql_error());
// Put the $clientdata query into an array we can work with
$data = mysql_fetch_array($clientdata, MYSQL_ASSOC);
// If the username and password matched, we should have one entry in our
// $clientdata array. If not, we should have 0. So, we can use a simple
// if/else statement
if(mysql_num_rows($clientdata) == 1){
// Start a new blank session. This will assign the user's server
// with a session with an idividual ID
session_start();
// With our session started, we can assign variables for a logged
// in user to use until they log out.
$_SESSION['username'] = $user;
// Then, redirect them to the profile page
header('***variable***);
}else{echo "The username and password don't match. Please go back and try again.
(Or you could redirect them to the login page again.)";}
<form action="loginscript.php" method="post">
Username:
<input type="text" name="username" /><br />
Password:
<input type="password" name="password" /><br />
<input type="submit" value="Submit" />
</form>
What I can't figure out is this:
Once I authenticate the user I need to pull that users unique url from mysql database and then redirect them there. I know this should be simple enough but every time I try something either I get errors or nothing at all. I wanted to try to pull the url from the database, set it to a session variable, and use header to redirect to that variable. I just don't enough about php and its syntax to pull it off. Thanks in advance for your help guys.