0

This is my code:

    ob_start();

    echo"<script type='text/javascript'>alert('Wrong username or password Please try again')</script>";
                header("Location:http://localhost/xampp/my_hospital/login.php");
ob_end_flush();

This code is located in a separate file called login_check.php.I want the alert to pop up and then it should redirect to login.php.The problem is the alert doesn't show up but the redirect works.

user1613360
  • 1,280
  • 3
  • 16
  • 42
  • 1
    Using `header()` in php will redirect the user before any HTML/JS is rendered to the client. The redirect would need to also exist in javascript via `window.location` or something similar. – Sean3z Apr 26 '14 at 02:34
  • Why don't you store the message in session, redirect on login failure and display the message on your login.php? – Jay Bhatt Apr 26 '14 at 02:42

3 Answers3

0

try this :

echo "<script type='text/javascript'>
        alert('Wrong username or password Please try again');
        location = 'http://localhost/xampp/my_hospital/login.php';
      </script>";
user1613360
  • 1,280
  • 3
  • 16
  • 42
aimadnet
  • 440
  • 4
  • 12
0

You cannot echo or output any character (even a blank space) before using header()

Functions that send/modify HTTP headers must be invoked before any output is made. Otherwise the call fails:

Warning: Cannot modify header information - headers already sent (output started at file:line)

Use meta refresh instead:

 <html xmlns="http://www.w3.org/1999/xhtml">    
  <head>      
    <title>The Tudors</title>      
    <meta http-equiv="refresh" content="5;URL='http://yoursite.tld/loginpage'" />    
  </head>    
  <body> 
    <p>Wrong username or password Please try again</p> 
  </body>  
</html>    

You can also use ob_start() :

<?php
ob_start();
echo "<script type='text/javascript'>alert('Wrong username or password Please try again');";
header("Refresh: 5; http://yoursite.tld/loginpage");
ob_end_flush();
?>
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
  • Check this http://stackoverflow.com/questions/3766353/interview-question-can-we-have-an-echo-before-header – user1613360 Apr 26 '14 at 02:42
  • Look at this one: http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php?answertab=active#tab-top – Pedro Lobito Apr 26 '14 at 02:44
0

Following method wait 10 seconds before redirecting the user.

ob_start();

header("Refresh: 10; url=/xampp/my_hospital/login.php");
echo"<script type='text/javascript'>alert('Wrong username or password Please try again');</script>"; 

ob_end_flush();
Himal
  • 1,351
  • 3
  • 13
  • 28