473,406 Members | 2,369 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How to get page to redirect after login

I'm trying to create a simple login page with php using session. when i want to access the index.php, the page successfully redirect to login page. then I put username and password based on the table in database. the problems arise when I press the submit button, username and pssword is accepted but the page is still in login page (not redirect to defined page in the code.

how this can happen? Is there something wrong in the code that I created? or something wrong with my php.ini settings? ANYONE please help me..

for the detail, the following is the code that I created.

1. login.php - the login form that submit to checklogin.php
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <title>Login</title>
  4. </head>
  5. <body>
  6. <form name="form1" method="post" action="checklogin.php">
  7. <dl>
  8.     <dt>Username</dt>
  9.         <dd><input type="text" name="username"></dd>
  10.     <dt>Password</dt>
  11.         <dd><input type="password" name="password"></dd>
  12.     <dt></dt>
  13.         <dd><input type="submit" name="Submit" value="Login"</dd>
  14. </dl>
  15. </form>
  16. </body>
  17. </html>
  18.  
2. checklogin.php - the page for check if username and password is correct or not
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. session_start();
  3. $server = "localhost";
  4. $username = "root";
  5. $password = "root";
  6. $db_name = "ecommerce";
  7. $db = mysql_connect($server,$username,$password) or DIE("connection failed !!");
  8. mysql_select_db($db_name) or DIE("no database !!");
  9. $login = mysql_query("select * from login where (username = '".$_POST['username']."') and (password = '".md5($_POST['password'])."')",$db);
  10. $rowcount = mysql_num_rows($login);
  11. if ($rowcount == 1){
  12.     $_SESSION['username'] = $_POST['username'];
  13. header("Location: index.php");
  14. }
  15. else
  16. {
  17. header("Location:login.php");
  18. }
  19. ?>
  20.  
3. index.php - the redirect page or the main page
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. session_start();
  3. if (!isset($_SESSION['username'])){
  4. header("Location:login.php");
  5. }
  6. echo"login success, this is index.php page<br>";
  7. echo"welcome, user<br>";
  8. echo"<a href=\"logout.php\">logout</a>";
  9. ?>
  10.  
I hope there is someone who can help provide a solution for me. thanks before.
Oct 10 '10 #1
2 2432
oranoos3000
107 100+
hello
for using header statement not must be any output before this statement i think you have a space befor <?php
in login.php file otherwise you must check thing is send to output before header statement
thanks alot
Oct 10 '10 #2
kovik
1,044 Expert 1GB
If the session is working, then it's likely not an issue with the output buffer. The problem is likely in your use of the "Location" header. The "Location" header requires a space after the colon (like all headers) and requires an absolute path, not a relative path. Using a relative path puts you at the risk of your header not working, as the browser isn't required to serve your malformed header "intelligently."
Oct 10 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Pavel Vetesnik | last post by:
Greetings! I have some troubles with session variables. I can easily create a session, but the variable I append to global session array ( $_SESSION) lasts only until the end of current...
0
by: oz | last post by:
hello, i publish my project in web server. but i have a problem about login. (i use visual studio 2005) For example, one person (eg: John) login X machine with "john" user name. if another...
6
by: joe t. | last post by:
The subject may sound a little cryptic, so i'll try my best to explain. Details are unavailable, as i am under a nondisclosure agreement, but i'm looking for general principles and tips, not...
15
by: bb nicole | last post by:
The company profile showed blank after user login and click the company profile button. Supposed the company profile should not be blank and will show the company information which call from...
2
by: Gordon Burditt | last post by:
I had this idea about preventing session fixation, and I'm wondering what anyone else thinks about it. The idea is, essentially, don't allow session ids that YOUR PHP didn't generate (and aren't...
9
by: Ben | last post by:
Hello, I'll bet this has been asked a million times but I can't seem to find a thread that gives the clear example I need. This PC has MySQL and IIS configured and running. The MySQL database is...
7
by: judge82 | last post by:
please I am learning how to use servlet. here is my code package TestWebApp; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.*; public class...
3
by: judge82 | last post by:
I am learning how to use servlet, I want create a session that when one logs in it will say "weclome 'username'" in the welcome page the first code I wrote (first below), when i click submit it...
1
progdoctor
by: progdoctor | last post by:
Friends.. Im a newbie to PHP. I want to develop a webbased application.According to the plan this app will be stored in a server and all workstation request the app to this server.My question is how...
22
by: macdalor | last post by:
HI, I hope someone can help. I'm trying to show the username of the logged in user on this menu.inc page and certain pages only when logged in. The result I have with the code below is no...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.