Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 136,252 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 2,227 people online right now. Registration is fast and FREE... Join Now!




Create User Specific Landing Pages Using PHP and MYSQL

 
Reply to this topicStart new topic

Create User Specific Landing Pages Using PHP and MYSQL

prodempsey
12 Oct, 2008 - 04:28 PM
Post #1

New D.I.C Head
*

Joined: 12 Oct, 2008
Posts: 1

I have this PHP script working in conjuction with mysql which delivers the user to a general landing page after they log in. However, I need to create a unique landing page that is specific to each user that logs in using mysql. Can anyone show me how to change the PHP generated code to so when user A logs in they will be forwarded to http://mywebsite.com/usera, and when user B logs in they will be forwarded to http://mywebsite.com/userb? I added a User Specific Landing Page (USLP) column in mysql named "uslp_value" without quotes but not sure how to inject into the PHP script.

Below is an example of the PHP I have generated with the general landing page, anything you can do will be much appreciated.

CODE
<?php

require 'init.php';
require 'mysqlvars.php';
require 'lib/form_functions.php';
require 'lib/db.php';

unset($error);

$dbConn = connectDB($dbHost, $dbUser, $dbPass, $dbDB);
if (!$dbConn) {
die('Database is currently down...please try again later');
}


if (isset($_POST['login'])) {
if (empty($_POST['username']) || empty($_POST['password'])) {
$error = 'Please enter your username and password';
}

else {
$result = mysql_query("SELECT email FROM wsd_members WHERE username = '" . prepareData($_POST['username']) . "' and password = MD5('" . prepareData($_POST['password']) . "')");
if ($result) {
if ($frow = mysql_fetch_row($result)) {
$email = $frow[0];
mysql_query("UPDATE wsd_members SET last_logged_on = NOW() WHERE username = '" . prepareData($_POST['username']) . "'");
session_start('MEMBERS');
$_SESSION['username'] = stripData($_POST['username']);
$_SESSION['email'] = $email;
session_write_close();

// landing page after login
header("Location: http://www.mywebsiteaddress.com");
exit();
}
else {
$error = 'Invalid username or password';
}
}
else {
$error = mysql_error();
}
}


}

include 'includes/header.php';

?>

<tr>
<td>
<table class="fg">
<tr>
<td class="title"><?php echo $APP->name ?> Login</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<table class="nb">
<tr>
<td><DIV>Please ensure cookies and Javascript are enabled in your browser</DIV></td>
</tr>

</table>
</td>
</tr>

<tr>
<td>
<table class="nb">
<?php if (isset($error)) { echo "<tr><td class=\"error\">$error</td></tr>"; } ?>

</table>
</td>
</tr>
<tr>
<td>
<table class="fg">
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<tr>
<td class="label">Username:</td>
<td class="field">
<input type="text" name="username" size="20" maxlength="20" value="<?php if (isset($_POST['username'])) { echo htmlChars($_POST['username']); } ?>">
</td>
</tr>
<tr>
<td class="label">Password:</td>
<td class="field">
<input type="password" name="password" size="16" maxlength="16" value="<?php if (isset($_POST['password'])) { echo htmlChars($_POST['password']); } ?>"> <a href="forgotten_password.php">Forgotten?</a>
</td>
</tr>

<tr><td class="label"> </td><td class="field"><input type="Submit" name="login" value="Login"></td></tr>
</form>
</table><br>
<DIV>Content you put here will appear below the members login box</DIV></td>
</tr>

<?php include 'includes/footer.php' ?>  







User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Create User Specific Landing Pages Using PHP And MYSQL
12 Oct, 2008 - 04:59 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,199



Thanked: 213 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Well first of all you don't want to redirect the user to separate landing pages, you want to customize the landing page to the person who logged in. The idea is that if user A logs in, the page they see next is the same page everyone gets, but the PHP code on that page renders all information and graphics specific to user A. If user B logs in, again they are taken to the same page but that page pulls all the data out of MySQL and customizes the page according to user B preferences.

In this situation the landing page is called a "view" into the data from MySQL (the model). The PHP code acts as the "controller" here determining what rules apply to the user logging in and what views to show them. This is the process behind the MVC (model-view-controller) design pattern.

The reason I am bringing this up is because 1) You don't want to have to create several pages for each user. 2) You want to code only one page for easy maintenance later and keep rules in one central location and 3) allows your site to extend easily without any work.

But if you INSIST on redirecting users to other pages, all you need to do is check the login is successful and issue the appropriate redirection using the header() function. But remember that header() cannot be used on any pages where content has already been sent or displayed to the user PRIOR to using the function. So that means no echo / print statements or displaying HTML output before using header(). Otherwise you are going to get a "headers already sent" error.

Good luck. smile.gif
User is offlineProfile CardPM
+Quote Post

spearfish
RE: Create User Specific Landing Pages Using PHP And MYSQL
12 Oct, 2008 - 07:59 PM
Post #3

Monkey in Training
Group Icon

Joined: 10 Mar, 2008
Posts: 746



Thanked: 2 times
Dream Kudos: 225
My Contributions
I would agree with Martyr in that it's best if you keep it all contained on one page. However, I can see a situation where you have files like landing_a.php, landing_b.php, etc. with the user's preference stored in the MySQL column.

Your options here are to a) use a switch statement to display the right content, or cool.gif use a redirect. Still, I think keeping it all on one page is the best. Here's the redirect code:

php

$uslp = $row['uslp_value'];
header("Location: http://www.yoursite.com/page_" . $uslp);
exit;


that will redirect to http://www.yoursite.com/page_x, assuming the value of the USLP is "x".
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 04:20AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month