Solved

mysql database - users login - redirect to their page

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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.
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now