Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

mysql database - users login - redirect to their page

Posted on 2011-09-24
5
Medium Priority
?
460 Views
Last Modified: 2012-05-12
i have a website where users can create and login to a website.
the database is a mysql.

i have it setup to
username
password
email

how can i get it so that when they create a username and it goes to the database,
to create a link related to that username to redirect them to page with their name on it?

example:  bob signs up
creates bob in the database
creates a page www.mysite.com/bob
redirects bob to that page.

thanks
0
Comment
Question by:webdott
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 
LVL 11

Expert Comment

by:maeltar
ID: 36594526
You could set a session super global with the users name and present that on each pahe, ensure you have session_start() on the very first line of each and every page that you need the global variable on.

So on your login page you would have :
(after the login was successful)
$_SESSION['user'] = $username;

Open in new window

http://php.net/manual/en/reserved.variables.session.php


To redirect to their page you could use the php header funtion like :

header("Location: http://www.example.com/{$_SESSION['user']}");

Open in new window


http://php.net/manual/en/function.header.php
0
 
LVL 11

Expert Comment

by:maeltar
ID: 36594528
I use this for a simple method :

One every page except login.php it starts :

session_start();
	if ($_SESSION['logged'] != 1) { // There was no session found!
	header("Location: login.php"); // Goes to login page.
	
	exit(); // Stops the rest of the script.
	}

Open in new window

0
 
LVL 11

Accepted Solution

by:
maeltar earned 2000 total points
ID: 36594533
Bah, keep hitting submit !!!

login.php

<?
session_start();


</head>
<body>


<?

	if (!isset($_POST['submit'])) { // The form has not been submitted.
		echo "<form action=\"login.php\" method=\"POST\">";
		echo "<table border=\"0\" width=\"50%\">";
		echo "<tr>";
		echo "<td colspan=\"2\">Login:</td>";
		echo "</tr>";
		echo "<tr>";
		echo "<td width=\"20%\">Username:</td><td width=\"80%\"><input name=\"username\" size=\"18\" type=\"text\" style=\"font-size:20pt;\"/>";
		echo "</tr>";
		echo "<tr>";
		echo "<td>Password:</td><td><input name=\"password\" size=\"18\" type=\"password\" style=\"font-size:20pt;\"/>";
		echo "</tr>";
		echo "<tr>";
		// echo "<td colspan=\"2\"><input type=\"submit\" name=\"submit\" value=\"submit\"";
        ?>
        <td>&nbsp;</td><td> <input type="submit" style="font-face: 'Arial'; font-size: 20pt;" value=" LOGIN NOW " name="submit"> </td>
        <?
		echo "</tr>";
		echo "</table>";
		echo "</form>";
	} else {
		$username = $_REQUEST['username'];
		$password = $_REQUEST['password'];
		$sql = "SELECT * FROM users where username = '$username' and password = '$password'";
		$q = mysql_query($sql);
		$r = mysql_num_rows($q); // mySQL query

		$row = mysql_fetch_row($q);
		if ($r == 1) { // There is something in the db. The username/password match up.
			$_SESSION['logged'] = 1; // Sets the session.
			$_SESSION['nickname'] = $row[1];
			$_SESSION['access_level'] = $row[4];
			$_SESSION['logged_in_as'] = $row[5];
			$_SESSION['usr_user_id'] = $_REQUEST['username'];
            

			echo "<p>Hi {$row[5]} Login Successful, you will be redirected in 5 secs</p>";
            ?>
            <div id="countDown" style="font-face: 'Arial'; font-size: 50pt; margin-left:100px;" ></div>
            <?
			echo "<a href=\"index.php\"><img src=\"images/btn_continue.png\" border=\"0\" /></a>";
            $device = '';
             header("refresh: 5; index.php");
        

			
			if (!mysql_query("insert into access_log (user_id, operation,platform,address)values('{$_SESSION['usr_user_id']}','User Logged in', '{$device}','{$ip}')"))
			{
				echo "OOps <br />";
			}
			if ($mail_system == 1 )
			{
			$sql = mysql_query("select * from messages where s_user_id = {$_SESSION['agent_id']} and flag = 'n'");
			
			$new_mails = mysql_fetch_row(mysql_query("select count(id) from messages where s_user_id = {$_SESSION['agent_id']} and flag = 'n'"));
			
			echo "<p>{$_SESSION['logged_in_as']}, you have <strong>{$new_mails[0]}</strong> new message(s) waiting</p>";
			echo " <a href=\"sysmail/index.php\">Check messages now</a>";
			}
			// header('Location : index.php');
			// exit();
			} else { // Invalid username/password.
            ?>
             <meta http-equiv="REFRESH" content="5;index.php">
            <?
			echo "Incorrect username and/or password";
            // echo "From Address {$ip}";
            
			}
}


?>
</body>
</html>

Open in new window


once the login script has run successfully you coud then redirect as you want to.

0
 
LVL 11

Expert Comment

by:maeltar
ID: 36594537
!!! Ignore line 53 that should not be there, I use Javascript to do the forwarding I uncommented that by mistake, for that to work you need comment line 47 and 49, then the redirect should work
0
 

Author Closing Comment

by:webdott
ID: 36891359
thanks for your help
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question