Password Protection & Redirect to User sepcific area

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
ckier
Forum Newbie
Posts: 4
Joined: Mon Aug 23, 2010 5:13 pm

Password Protection & Redirect to User sepcific area

Post by ckier »

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

Code: Select all


<?php
$conn = mysql_connect("localhost","*user*","*password*");
$db = mysql_select_db("*database_name*");

loginscript.php

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.)";}

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.
shawngoldw
Forum Contributor
Posts: 212
Joined: Mon Apr 05, 2010 3:38 pm

Re: Password Protection & Redirect to User sepcific area

Post by shawngoldw »

Are you just looking for what to put in header()?

Code: Select all

header("Location: http://url");

Shawn
ckier
Forum Newbie
Posts: 4
Joined: Mon Aug 23, 2010 5:13 pm

Re: Password Protection & Redirect to User sepcific area

Post by ckier »

Hi Shawn,

Thanks for the help but I need to do a little bit more than that. Basically I already have the login part done and I have stored the username as a session variable. What I need now, is to do a mysql query for the corresponding url and then then redirect the user there. Since my database has a different url for each user I need to somehow grab that specific users url, store it as a variable, and then redirect to that variable. I hope that kind of explains what I am looking to do. Again thanks so much for taking the time to help.
Trahb
Forum Commoner
Posts: 36
Joined: Sat Jan 30, 2010 9:09 pm

Re: Password Protection & Redirect to User sepcific area

Post by Trahb »

Code: Select all

$q = mysql_query("SELECT * FROM `accounts` WHERE `username` = '".$_SESSION['username']."'");
$info = mysql_fetch_array[$q];
$_SESSION['url'] = $info['url'];
I feel like that's all you need, but I don't quite understand what you're looking to do. With that, all you need to do is send them to that url. so header("Location: ".$_SESSION['url']); or something like that? Lemme know how this works for you.
ckier
Forum Newbie
Posts: 4
Joined: Mon Aug 23, 2010 5:13 pm

Re: Password Protection & Redirect to User sepcific area

Post by ckier »

Yeah that is really pretty much all I need. I think one of my major hurdles is getting all the syntax right. Only problem is now I get this error message.

Parse error: syntax error, unexpected '[' in /home/chrikie6/public_html/redirect/loginscript.php on line 16
shawngoldw
Forum Contributor
Posts: 212
Joined: Mon Apr 05, 2010 3:38 pm

Re: Password Protection & Redirect to User sepcific area

Post by shawngoldw »

It's probably because it is supposed to be like this:

Code: Select all

$q = mysql_query("SELECT * FROM `accounts` WHERE `username` = '".$_SESSION['username']."'");
$info = mysql_fetch_array($q);
$_SESSION['url'] = $info['url'];
Trahb
Forum Commoner
Posts: 36
Joined: Sat Jan 30, 2010 9:09 pm

Re: Password Protection & Redirect to User sepcific area

Post by Trahb »

I need to know what line 16 contains to be able to help ya there.
ckier
Forum Newbie
Posts: 4
Joined: Mon Aug 23, 2010 5:13 pm

Re: Password Protection & Redirect to User sepcific area

Post by ckier »

Thanks so much guys. Everything works perfect now.
Trahb
Forum Commoner
Posts: 36
Joined: Sat Jan 30, 2010 9:09 pm

Re: Password Protection & Redirect to User sepcific area

Post by Trahb »

shawngoldw wrote:It's probably because it is supposed to be like this:

Code: Select all

$q = mysql_query("SELECT * FROM `accounts` WHERE `username` = '".$_SESSION['username']."'");
$info = mysql_fetch_array($q);
$_SESSION['url'] = $info['url'];

Mhm. My bad. Effed that up, I was typin in a hurry haha.

Just use his code it should work flawlessly, thanks for catching that mistake shawn
shawngoldw
Forum Contributor
Posts: 212
Joined: Mon Apr 05, 2010 3:38 pm

Re: Password Protection & Redirect to User sepcific area

Post by shawngoldw »

No problem, silly stuff happens in programming

Shawn
Post Reply