Solved

mysql database - users login - redirect to their page

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Register AutoHotkey 12 53
Re-position the objects 7 96
Cordova Camera plugin fails 2 93
How to loop bootstrap columns which contain database records 9 19
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
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…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

911 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

21 Experts available now in Live!

Get 1:1 Help Now