You wouldn't use a header for this, try echoing the meta redirect tag.
if ($stmtselect->rowCount() > 0) {
echo '<meta http-equiv = "refresh" content = "5; url = https://www.example.com" />';
$_SESSION['accounts'] = $user;
echo 'You have signed in successfully!';
die();
}else {
echo '<meta http-equiv = "refresh" content = "5; url = https://www.example.com/otherpage" />';
$_SESSION['accounts'] = $user;
echo 'Incorrect Username or Password or Email';
die();
}
It doesn't make a lot of sense to use AJAX if you're just going to redirect the user to another page immediately after it's finished. Here's a more conventional login procedure based on your code, which doesn't use AJAX at all.
Summary of changes:
type="submit"
if
which checks if the form has been submitted before trying to process it.login.php:
<?php
error_reporting(-1);
session_start();
if(isset($_SESSION['id16365171_hello_world_accounts'])){
header("Location: index.php");
die();
}
require_once('loginconfig.php');
//process the form submission, if any
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$email = $_POST['email'];
$username = $_POST['username'];
$password = $_POST['password'];
$message = "";
if (isset($email) && isset($username) && isset($password)) {
$sql = "SELECT * FROM users WHERE username=? and password=? and email=? LIMIT 1";
$stmtselect = $db->prepare($sql);
$result = $stmtselect->execute([$username, $password, $email]);
$user = $stmtselect->fetch(PDO::FETCH_ASSOC);
if ($user) {
$_SESSION['id16365171_hello_world_accounts'] = $user;
header("Location: index.php");
die();
}
else
{
$message = "Incorrect Username or Password or Email";
}
}
else
{
$message = "Please enter all required details";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="google-site-verification" content="0J0VoOQKJVdlFn7Us8_s97YvAXLirkBVrJ75FGLe_Ds" />
<title>Hello World | Login</title>
<link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.1/css/all.css" integrity="sha384-50oBUHEmvpQ+1lW4y57PTFmhCaXp0ML5d60M1M7uH2+nqUivzIebhndOJK28anvf" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="css/styles.css">
</head>
<body>
<div class="container h-100">
<div class="d-flex justify-content-center h-100">
<div class="user_card">
<div class="d-flex justify-content-center">
<div class="brand_logo_container">
<img src="img/logo.png" class="brand_logo" alt="Programming Knowledge logo">
</div>
</div>
<div class="d-flex justify-content-center form_container">
<?php if ($message != "") echo $message; ?> <!-- you might want to add some more HTML round that to make it display nicely -->
<form method="POST">
<div class="input-group mb-2">
<div class="input-group-append">
<span class="input-group-text"><em class="fas fa-user"></em></span>
</div>
<input type="text" name="username" id="username" class="form-control input_user" placeholder="Username" required>
</div>
<div class="input-group mb-2">
<div class="input-group-append">
<span class="input-group-text"><em class="fas fa-key"></em></span>
</div>
<input type="password" name="password" id="password" class="form-control input_pass" placeholder="Password" required>
</div>
<div class="input-group mb-1">
<div class="input-group-append">
<span class="input-group-text"><em class="fas fa-inbox"></em></span>
</div>
<input type="email" name="email" id="email" class="form-control input_pass" placeholder="Email" required>
</div>
<div class="form-group">
<div class="custom-control custom-checkbox">
<input type="checkbox" name="rememberme" class="custom-control-input" id="customControlInline">
<label class="custom-control-label" for="customControlInline">Remember me</label>
</div>
</div>
</div>
<div class="d-flex justify-content-center mt-1 login_container">
<button type="submit" name="button" id="login" class="btn login_btn">Login</button>
</div>
</form>
<div class="mt-3 mb-1">
<div class="d-flex justify-content-center links">
Don't have an account? <a href="temp/home.php" class="ml-2">Sign Up</a>
</div>
<div class="d-flex justify-content-center">
<a href="#">Forgot your password?</a>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
P.S. Please also take note of the various security points I made in the comments above.
Your javascript says
{-code-1}
Which means that if you echo out1
then do something.
You should either change the php to echo out1
or change the javascript to
if($.trim(data) === "You have successfully signed in!"){
The the redirect would work.
One more thing- Your redirect would be better if you did
setTimeout(function(){ document.location.replace("http://www.example.com"); }, 1000)
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
JavaScript is a multi-paradigm language that supports event-driven, functional, and mandatory (including object-oriented and prototype-based) programming types. Originally JavaScript was only used on the client side. JavaScript is now still used as a server-side programming language. To summarize, we can say that JavaScript is the language of the Internet.
https://www.javascript.com/
CSS (Cascading Style Sheets) is a formal language for describing the appearance of a document written using a markup language.
It is mainly used as a means of describing, decorating the appearance of web pages written using HTML and XHTML markup languages, but can also be applied to any XML documents, such as SVG or XUL.
https://www.w3.org/TR/CSS/#css
Bootstrap is not exclusively a CSS framework, but its most popular features are CSS-centric. These include a powerful grid, icons, buttons, map components, navigation bars, and more.
https://getbootstrap.com/
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.