Jump to content

PHP / Database login redirect


Roboff

Recommended Posts

I am still learning PHP and have read and tried many different things but can't get any of them to work. I need help with this one, PLEASE!

I have a working login script that uses PHP and a database for my login. My database has a table name clientpage with fields  id, username, password, clientpage.  When the user logs in they are redirected to userarea.php.

header("Location: userarea.php")

 

What I need to know is how I can redirect each one of my clients to their own page depending on the username they enter. The clients pages are very unique so I can't redirect to the same page and just change the data.

 

Example:

 

id - 1

username - cologicsystems

password - secret1

clientpage - client1.php

 

id - 2

username - nocore

password - secret2

clientpage - client2.php

 

and so on...... PLEASE HELP. I need this to work in 2 days. Thank you

 

 

MY PAGES

 

 

login.php

 

<?php

include 'functions.php'; 

if (loggedin())
{
 header("Location: userarea.php");
 exit();
 }

if($_POST['login'])
{



$username = $_POST['username'];
$password = $_POST['password'];
$rememberme = $_POST['rememberme'];

if ($username&&$password)
{


    $login = mysql_query("SELECT * FROM users WHERE username='$username'");
	while ($row = mysql_fetch_assoc($login))

	{
	 			$db_password = $row['password'];
				if ($password==$db_password)
						 $loginok = TRUE;
				else
						$loginok = FALSE;
					}	
				if ($loginok==TRUE)
				{

				 	 if ($rememberme=="on")
					 		setcookie("username", $username, time()+7200);
					 else if ($rememberme=="")
					 		$_SESSION['username']=$username;


					header("Location: userarea.php");
					exit();

				}
				else
						echo "Username/password combination incorrect.<p />";


				}
				else
				   die("Hit back and enter a username and password.");
	}




?>

<form action="login.php" method="POST">
Username:<br/>
<input type="text" name="username"><p />
Password:<br/>
<input type="password" name="password"><p />

<input type="checkbox" name="rememberme"> Authorized user<br /><br />
<input type="submit" name="login" value="Log in">

</form>

 

 

functions.php

 

<?php


//session
session_start();

// connect to database
$connect = mysql_connect("localhost","arc_robert","arc") or die("Could not connect");
mysql_select_db("arc_rememberme") or die("Could not find db"); 

//login check function
function loggedin()

{
  if (isset($_SESSION['username'])||isset($_COOKIE['username']))
{
   $loggedin = TRUE; 

	return $loggedin;
}
}


?>

 

userarea.php

 

<?php

include 'functions.php';

if (!loggedin())
{
header("Location: login.php");
exit();
}

?>


You are logged in!<p />

<a href="logout.php">Log out</a>

 

logout.php

 

<?php


session_start();

//destroy session
session_destroy();

//unset cookies
setcookie("username","",time()-7200);

header("Location: login.php");


?>


 

[attachment deleted by admin]

Link to comment
Share on other sites

When you fetch the data from the db and check if the user has logged in succesfully. If he logged in just use the database information for the clientpage to redirect the user. And please use code tags, thats a mess to read long codes without them.

 

if ($loginok == TRUE)
{
header('Location: ' . $row['clientpage']);
}

Link to comment
Share on other sites

Thanks for the reply, but I tried that already. But when it redirects it goes back to the main index page instead of the database clientpage. No errors at all, it just doesnt redirect to what is in the database. Still not working....

Link to comment
Share on other sites

The $username session variable is set in the login.php. When it redirects to the userarea.php the username session is still set. What i need to know is how to query the $username session and then have it redirect to different pages depending what the $username session is? Any direction on how to do this would be great. Please teach... or tell me what im doing wrong.

Link to comment
Share on other sites

When you fetch the data from the db and check if the user has logged in succesfully. If he logged in just use the database information for the clientpage to redirect the user. And please use code tags, thats a mess to read long codes without them.

 

if ($loginok == TRUE)
{
header('Location: ' . $row['clientpage']);
}

 

If you need this $row['clientpage']  in another script, you have to put it in a session-variable as well.

Link to comment
Share on other sites

Okay, I have worked on this for 5 days straight and still cant make it work. I am out of time and have to have this working asap.  I have tried to learn it myself and I read 100's of different tutorials, but cant make it work, and now I am out of time. So...... Where is the best place online to pay someone to fix this for me quickly. I need it done by tomorrow.

 

Also thank you to all who answered and tried to help.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.