why php executes header statement without executing the echo statement [duplicate] - javascript

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 2 years ago.
$count = mysqli_num_rows($result);
if($count == 1){
echo "<h1><center> Login successful </center></h1>";
} else{
echo "<script>alert('login failed! invalid username or password');</script>";
header("Location: index.php") ;
}
I want to display the alert message as login failed and if the user press ok then it should again go back to the login page.I tried above code but ,that doesn't work.
the browser moves to the login page without showing the alert message.
Is there any alternative ways for this?

echo is working well, but the code is sync in php right there. That is why header runing with echo, but header redirecting you to index.php and echo was seen for a while.
You can store your message to a SESSION and show it on index.php if the msg is exists in SESSION:
.
.
else{
session_start();
$_SESSION['msg'] = 'Login failed! invalid username or password';
header("Location: index.php");
index.php:
session_start();
if(isset($_SESSION['msg'])){
echo $_SESSION['msg'];
unset($_SESSION['msg'];
}

Related

Javascript Alert in PHP file -- trying to get it to redirect back to login page after alert notifies user of failed credentials

When I click on "ok" on the alert box after user credentials are submitted in error, it doesn't redirect me back to the login page, instead it directs me to my authenticate.php file on my browser. How do I get it to direct back to the login page for the user to re enter their credentials? Below is my code, Thanks!
if ($stmt->num_rows > 0) {
$stmt->bind_result($id, $password);
$stmt->fetch();
// Account exists, now we verify the password.
// Note: remember to use password_hash in your registration file to store the hashed passwords.
if (password_verify($_POST['password'], $password)) {
// Verification success! User has loggedin!
// Create sessions so we know the user is logged in, they basically act like cookies but remember the data on the server.
session_regenerate_id();
$_SESSION['loggedin'] = TRUE;
$_SESSION['name'] = $_POST['username'];
$_SESSION['id'] = $id;
header('Location: home.php');
// THIS CODE NEEDS TO BE EDITED TO DISPLAY POP UPS AND NOT GO TO NEW PAGE
} else {
// Incorrect password
echo '<script language="javascript">';
echo 'alert("Incorrect Password")';
echo '</script>';
} else {
// Incorrect username
echo '<script language="javascript">';
echo 'alert("Incorrect Username")';
echo '</script>';
}
$stmt->close();
}
?>
This code may help you,
echo ("<script language='JavaScript'>
window.alert('Incorrect Password');
window.location.href='http://someplace.com';
</script>");

php redirecting to a page with alert dialog

I need to show a alert dialog from php if registration successfull. As well as I want to redirect to another page. my php code is here:
if($conn->query($sql)===TRUE){
echo "<script>alert('Registration Successfull');</script>";
header('Location: index.php');
exit();
}
but I get an warning message which says:
Warning: Cannot modify header information - headers already sent by
(output started at
/home/bdacademichelp/public_html/medcino.com/signup_back.php:19) in
/home/bdacademichelp/public_html/medcino.com/signup_back.php on line
20
line 19 is the echo line which shows the alert message
you can print message in alert and redirect as below:
echo "<script>alert('Success');document.location='index.php'</script>";
In your code you already send something as echo so you cannot use header after that.
Using a javascript alert is going to delay the redirect until the user clicks OK. What you probably want to do is display the alert in your index.php when there has been a successful subscription. There are a few different ways of passing this information but the most common way is to pass the success message in the session.
// page1.php
<?php
session_start();
if($conn->query($sql)===TRUE){
$_SESSION['message'] = "Registration successful";
header('Location: index.php');
exit();
}
session_write_close ();
?>
// index.php
<?php
session_start();
if (isset($_SESSION['message'])) {
$show_message = $_SESSION['message'];
$_SESSION['message'] = null;
}
session_write_close ();
// ...
if (isset($show_message)) {
echo "<script>alert('{$show_message}');</script>";
}
Other alternatives are to pass the data in the URL such as index.php?message=Registration%20Successful or passing the message in a cookie.

how to load the home page after done some function [duplicate]

This question already has answers here:
How do I make a redirect in PHP?
(34 answers)
Closed 7 years ago.
I have to reload automatically to my home after done following coding
if($sql)
{
echo "Updated successfully";
}else {
echo "Con not update" . mysql_error();
}
this is process.php page here I am writing some coding after executing my query I need to redirect to my home.php page with pop up message if $sql updated successfully message(successfully) if not error message.
Also, in pure PHP, without writing a piece of JavaScript code, you can do this:
Header('Location: home.php');
This code is plenty functional on PHP an it redirects you to the requested page.
You need to use JavaScript for both alert and redirection.
if($sql) {
echo "<script>alert('Updated successfully');
window.location.href='home.php';
</script>";
}
else {
echo "Con not update" . mysql_error();
}

Mysql database not updating when PHP script executes

I have a timeout function that runs correctly except for one thing. The field that checks if a user is currently logged in does not update once the script runs. I was testing it all week and got it to actually work, but when I tried to test it today, I stopped updating the field. Can anyone explain why? I also have a logout button that does a jQuery call which runs a logout.php script when the button is clicked which works perfectly. Could this be why the timeout script has stopped updating the database? I don't think that could what it is, but I could be wrong.
timeout script(php)
<?php
require("../includes/header.php");
$now = time();
$expires = $_SESSION["expire"] + 30;
$user = $_SESSION["id"];
if(!isset($_SESSION["expire"]) || $expires > $now){
$_SESSION["expire"] = $now;
}
else{
mysqli_query($connect, "UPDATE `$user_table` SET `logged_in`=0 WHERE `user_id`='$user_id'");
session_unset();
session_destroy();
mysqli_close($connect);
header("Location: timed_out.php");
}
?>
logout script(js & php)
$(document).ready(function(){
$("#logout").on("click", function(){
$.post("../php/logout.php", {}, function(response){
if(response.success == "1"){
location.replace("../pages/logged_out.php");
}
}, "json");
})
})
logout script(php)
<?php
ob_start();
require("../includes/header.php");
$user_id = $_SESSION["id"];
ob_clean();
mysqli_query($connect, "UPDATE `$user_table` SET `logged_in`=0 WHERE `user_id`='$user_id'");
session_unset();
session_destroy();
mysqli_close($connect);
echo json_encode(array("success"=>1));
?>
Another question I have is. Would it be beneficial to do the timeout script in jQuery instead of PHP? If so, how would I go about doing that?
I think you need some simple debuggin ;)
Your login script says in the query:
WHERE `user_id`='$user_id'
but the code a bit higher says
$user = $_SESSION["id"];
That's NOT the same variable ;)

JavaScript Alert not working properly

I am showing an alert using javascript in my php page. It is not showing alert box. Kinldy guide me how to make it work fine
if($sql && $sql2 && $sql3)
{
echo "<script> alert('Deleted successfully!');</script>";
$myURL = 'students_list.php';
header('Location: '.$myURL);
exit;
else
{
echo "<script> alert('Temporary problem, try again!');</script>";
}
?>
It is deleting data from database but not showing alert. Tell me how to make it work fine.
do this
<script> alert('Deleted successfully!');window.location='students_list.php'</script>
like this when the alert is closed you redirect.
You are first trying to display an alert:
echo "<script> alert('Deleted successfully!');</script>";
then to issue a HTTP redirection:
$myURL = 'students_list.php';
header('Location: '.$myURL);
These functions are incompatible, because of the way HTTP redirects work:
If you have output buffering enabled, a redirection response (HTTP 302) will be generated, along with your alert script as body... however the body of redirects is ignored by browsers supporting them (i.e., all of them, except for example a curl without the -L option).
If you don't have output buffering enabled, the script will output the JS code for the alert, but could not output the redirection anymore because at that point HTTP headers have already been sent out. Depending on the settings, a warning will be displayed in the client's browser.
A possible solution for that is to issue no redirect and use JavaScript to redirect the client, something like:
echo "<script>\nalert('Deleted successfully!');\n";
echo "location = 'students_list.php';</script>";
exit;
Check this it works perfectly
<?php
$sql=1;
$sql2=2;
$sql3=3;
if($sql && $sql2 && $sql3)
{
echo "<script> alert('Deleted successfully!');window.location='students_list.php'</script>";
exit;
} else
{
echo "<script> alert('Temporary problem, try again!');</script>";
}
?>

Categories