Solved

mysql database - users login - redirect to their page

Posted on 2011-09-24
5
450 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 500 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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 viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

696 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