5

I got a little problem. When I got my PHP script without header it's fine, I am getting javascript alert box. But when I use header before alert it's not working. It's redirecting me like it should, but it's not showing any box. Could someone help me?

if ( $pkt < 1 OR $user_id == 0) {
    header("Location: http://dunno.com/file.php");
    $message = 'This is a message.';

echo "<SCRIPT> //not showing me this
alert('$message');
</SCRIPT>";
    mysql_close();
}

And here's the one which work (but without heading)

if ( $pkt < 1 OR $user_id == 0) {
    $message = 'This is a message.';

echo "<SCRIPT> //showing me
alert('$message');
</SCRIPT>";
    mysql_close();
}

I would be thankful for help :)

@edit Maybe is there any command to make alert wait until whole browser load? (if it's the problem)

2
  • why do you need the header? Commented Apr 24, 2014 at 14:30
  • What is the purpose of the alert - should the redirect happen after the use clicks ok? what if the user clicks cancel? Commented Apr 24, 2014 at 14:30

7 Answers 7

5

Maybe try redirect using JavaScript.

if ( $pkt < 1 OR $user_id == 0) {
    $message = 'This is a message.';

    echo "<SCRIPT> //not showing me this
        alert('$message')
        window.location.replace('url of the page');
    </SCRIPT>";
    mysql_close();
}
Sign up to request clarification or add additional context in comments.

Comments

2

I know this is a old post, but here is how I made it in a similar example. This example checks if the administrator has logged in from a specific IP address, if it returns false it should redirect the user back to the login page and show an error message.

User has logged in page:

if ($_SERVER['REMOTE_ADDR'] == "ip address"){
    $admin = True;
}else{
    session_start();
    $_SESSION['errorMessage'] = "WARNING: You dont have access to this area.";
    header("Location: login.php");
}

login.php:

session_start();

if(isset($_SESSION['errorMessage'])){
    echo "<script type='text/javascript'>
            alert('" . $_SESSION['errorMessage'] . "');
          </script>";
    //to not make the error message appear again after refresh:
    session_unset($_SESSION['errorMessage']);
}

Comments

0

this is a redirect: header("Location: http://dunno.com/file.php");

the code below wont work

2 Comments

whats in the file.php ? maybe include it ? whats your intention ?
It's about HTML form on my page which exe an PHP script. When someone send it and he's missing (for example id) I want him to get this alertbox.
0

Your error is in the structure of your code. PHP, even in OOP is procedural and will run each line one after the other. So the way you've structured your code means the page is redirected before your echo. A better method is to use the javascript alert box to redirect your page using:

var prompt=window.confirm("message here. \r\n"+"Do you want to redirect?");
if(prompt)document.location.href='http://dunno.com/file.php');

Comments

0

Aaroniker is correct with the redirect comment. One way to make it work would be to send the message with a session variable and then add the message creation to the next page like so.

session_start();
if ( $pkt < 1 OR $user_id == 0) {
$_SESSION['message']= "this is a message";
header("Location: http://dunno.com/file.php");

On file.php:

session_start();
echo "<SCRIPT> //not showing me this
alert($_SESSION['message']);
</SCRIPT>";
    mysql_close();
}

Another option would be to pass them with you header. Like so:

if ( $pkt < 1 OR $user_id == 0) {
$message= "this is a message";
header("Location: http://dunno.com/file.php?message=" . $message . ");

Then on file.php:

echo "<SCRIPT> //not showing me this
alert($_GET['message']);
</SCRIPT>";
    mysql_close();
}

Comments

0

Use $_SERVER 'php_self'. It will allow you to create script on same page where you want alert. In this way you won't have to add header.

Here's an example:

<form name="login" action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST" accept-charset="utf-8" class="form_class">
                        <ul>
                            <li>
                                <label for="usermail">Username</label>
                                <input type="text" name="username" placeholder="Enter username" required>
                            </li>
                            <li>
                                <label for="password">Password&nbsp;</label>
                                    <input type="password" name="password" placeholder="Enter your password" required>
                            </li>
                            <li id="custom">
                                <input type="submit" value="Sign In">
                            </li>
                        </ul>
                    </form>


$sqluname="root";
$sqlpword="hrhk";
$database="registration";
$server="127.0.0.1";
$db_handle=mysql_connect($server,$sqluname,$sqlpword,$database);
$db_found = mysql_select_db($database, $db_handle);

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $uname = $_POST["username"];
    $pword = $_POST["password"];

    $ulength=strlen($uname);
    $plength=strlen($pword);

        if($ulength > 5){
            $sqlquery="SELECT * FROM members WHERE username='$uname' AND password='$pword'";
            $result=mysql_query($sqlquery);
            $num_rows=mysql_num_rows($result);
                if ($num_rows >0) {
                    echo "<script>alert('Logged In');</script>";

                    session_start();
                        $_SESSION['sid']=$uname;
                        header("location:Yahoo/yahoo.php");
                }
                else{

                    echo "<script>alert('Wrong Username or Password!');</script>";
                }
        }
        else{


            echo"<script>alert('Username should be greater than 5 characters.');</script>";
        }

}

?>

Comments

0

The below header function will run a 302 redirect by default and redirect the client to http://dunno.com/file.php so any code after the code begin processed after the header function will not be seen as the client is redirected from the page this code is on.

 header("Location: http://dunno.com/file.php");

Please read up further on the header function here: http://www.php.net/manual/en/function.header.php

You could make you program sleep/wait for a few seconds(lets say 10 seconds for this example) before redirecting if you need the message to display like so:

if ( $pkt < 1 OR $user_id == 0)
{
    $message = 'This is a message.';

    echo "<SCRIPT> //not showing me this
       alert('".$message."');
    </SCRIPT>";

    sleep(10);

    mysql_close();

    header("Location: http://dunno.com/file.php");
}

3 Comments

This worked somehow better than the other methods, but I don't get the error message? It's just a blank message box "page says...".
@saltcracker, made a minor adjustment to it there, see if that makes any difference.
I made it after some editing, the problem was that I had to start a session. I'll make a new answer describing how I made it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.