0
if ($stmtselect->rowCount() > 0) { header("refresh:5;url=https://www.websitename.com"); $_SESSION['accounts'] = $user; echo 'You have signed in successfully!'; die(); }else { header("refresh:5;url=index.php"); echo 'Incorrect Username or Password or Email'; die(); } 

I have the header first so why doesn't it work? Just so you know, this page is a form. It submits stuff to me. If it works then I want it to redirect after the echo.

I get this error.

jquery-3.3.1.min.js:2 [Violation] 'load' handler took 1365ms 

It might have to do with the fact that the echo is in the form of an error. Like this I guess.

Echo

If not then I don't know. Please help.

EDIT-

Here's the javascript.

<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script> <script type="text/javascript" src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script> <script> $(function(){ $('#login').click(function(e){ var valid = this.form.checkValidity(); if(valid){ var username = $('#username').val(); var password = $('#password').val(); var email = $('#email').val(); } e.preventDefault(); $.ajax({ type: 'POST', url: 'jslogin.php', data: {username: username, password: password, email: email}, success: function(data){ alert(data); if($.trim(data) === "1"){ // setTimeout(' window.location.href = "index.php"', 1000); setTimeout(' window.location.href = "https://kaden-tech.github.io"', 1000); } }, error: function(data){ alert('There were errors while doing the operation.'); } }); }); }); </script> 

Please don't say anything about password hashing.

Edit-

Index.php

<?php error_reporting(0); session_start(); if(!isset($_SESSION['id16365171_hello_world_accounts'])){ header("Location: login.php"); die(); } if(isset($_GET['logout'])){ session_destroy(); unset($_SESSION); header("Location: login.php"); } ?> <!DOCTYPE> <html lang="en"> <head> <meta name="google-site-verification" content="0J0VoOQKJVdlFn7Us8_s97YvAXLirkBVrJ75FGLe_Ds" /> <title>Welcome</title> </head> <body> <p>Welcome to index</p> <a href="index.php?logout=true">Logout</a> </body> </html> 

login.php

<?php error_reporting(-1); session_start(); if(isset($_SESSION['id16365171_hello_world_accounts'])){ header("Location: index.php"); } ?> <!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"> <form> <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="button" 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> <script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script> <script type="text/javascript" src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script> <script> $(function(){ $('#login').click(function(e){ var valid = this.form.checkValidity(); if(valid){ var username = $('#username').val(); var password = $('#password').val(); var email = $('#email').val(); } e.preventDefault(); $.ajax({ type: 'POST', url: 'jslogin.php', data: {username: username, password: password, email: email}, success: function(data){ alert(data); if($.trim(data) === "1"){ // setTimeout(' window.location.href = "index.php"', 1000); setTimeout(' window.location.href = "https://kaden-tech.github.io"', 1000); } }, error: function(data){ alert('There were errors while doing the operation.'); } }); }); }); </script> </body> </html> 

jslogin.php

<?php error_reporting(-1); session_start(); require_once('loginconfig.php'); $email = $_POST['email']; $username = $_POST['username']; $password = $_POST['password']; //$emailval = ['email']; //$check = (strpos($emailval,'@')); //if ($check === false){ // echo 'We require valid email'; //}else{ //$conn = mysqli_connect("localhost","*********","**********","*************"); $sql = "SELECT * FROM *********** 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 ($stmtselect->rowCount() > 0) { echo '<meta http-equiv = refresh content = 5; url = https://www.example.com />'; $_SESSION[`*********`] = $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(); } 

loginconfig.php

<?php $db_user = "root"; $db_pass = "password"; $db_name = "database name"; $db = new PDO('mysql:host=localhost;dbname='. $db_name . ';charset=utf8', $db_user, $db_pass); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

This is my code.

Please find a way to stop ajax and allow this to be functional.

35
  • 3
    I'm not 100% certain if refresh is a header in any official spec. I know it was used in meta tags and that some browsers supported it, but you might be better with a Location directive which is supported everywhere. Commented Mar 12, 2021 at 18:45
  • refresh works, I used it for another webpage today. It just that this part isnt working. Commented Mar 12, 2021 at 18:47
  • I'd make sure that your PHP is set to display warnings and errors, just in case there's a "headers already sent" warning in there. I'd also include some exit or die after you are done with echo to make sure no other code is changing your headers (for instance, if you happened to be caught in an OB) Commented Mar 12, 2021 at 18:52
  • i did that. Still doesn't work. Commented Mar 12, 2021 at 18:55
  • 1
    For the first refresh, I'm almost positive that you need to include the full protocol, so header("refresh:5;url=https://example.com"); Commented Mar 12, 2021 at 19:05

3 Answers 3

0

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(); } 
Sign up to request clarification or add additional context in comments.

2 Comments

I tried this. It doesn't redirect only says the code.
i sent you an email
0

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:

  • make the form button type="submit"
  • make the form do a POST
  • remove all JavaScript code
  • delete jsLogin.php, and move relevant code from it to login.php and wrap inside an if which checks if the form has been submitted before trying to process it.
  • output an error message if login fails. If it succeeds just set the session variables and redirect to index.php

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.

Comments

0

Your javascript says

if($.trim(data) === "1"){ 

Which means that if you echo out 1 then do something.

You should either change the php to echo out 1 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) 

Comments