473,569 Members | 2,793 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2437
oranoos3000
107 New Member
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 Recognized Expert Top Contributor
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
3930
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 function. I prepared an example that can demonstrate the problem (see code bellow): The program flow is this: (1) It starts with "session_start()" and...
0
842
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 person (eg: Mike) want to login Y machine his user name(mike), he authorized "john" user name, automaticly.
6
2107
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 necessarily fixes for existing code. There is a website that requires me to log in using a web-form. Obviously, POST vars are sent and verified and on...
15
2270
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 database. I use the session in login page. Thus, in the begining of companyProfile's coding, i put session_start();. What is the problem of my php code??...
2
2631
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 yet expired) to log in. That way if someone sticks a made-up session ID on a URL, it won't matter, unless it happens to correspond to an active...
9
3573
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 "myDB" with a table "myUsers" with fields "Username" and "Password". I also have the MySQL ODBC driver loaded with a DSN "dsnMySQL" setup. ...
7
2205
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 RegLoginServlet extends HttpServlet {
3
1410
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 takes me to the next page, but I want to use session to create a msg that will say "welcome username" in that next page. so in the second code i wrote...
1
1230
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 can i cover the login issues such as : if the user already logged then no one else can log in with that username again and users can login in the...
22
2854
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 username showing, just "Hi !" (the "login" link disappear as planned) and all pages showing (logged and un-logged) thank you for your help ...
0
7612
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7922
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8119
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7668
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5509
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3637
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2111
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1209
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.