Solved

mysql database - users login - redirect to their page

Posted on 2011-09-24
5
445 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
  • 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Formating field inside mysql query 2 31
Find unused columns in a table 12 73
MySQL-Design Help 12 42
How to use 2 ON statements in inner join 3 26
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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 …

749 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