为什么重定向不起作用?你会怎么修?

如何解决为什么重定向不起作用?你会怎么修?

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();
}

我先有标题,为什么它不起作用? 请注意,此页面是一个表单。它向我提交东西。如果它有效,那么我希望它在回声后重定向。

我收到此错误。

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

这可能与回声是错误形式的事实有关。我猜是这样的。

Echo

如果不是那么我不知道。请帮忙。

编辑-

这是 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>

请不要谈论密码散列。

编辑-

索引.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>

登录.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',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();
}

登录配置文件

<?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);

这是我的代码。

请找到一种方法来停止 ajax 并使其正常运行。

解决方法

您不会为此使用标头,请尝试回显元重定向标记。

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();
}
,

如果您只是在完成后立即将用户重定向到另一个页面,那么使用 AJAX 没有多大意义。这是基于您的代码的更传统的登录过程,它根本不使用 AJAX。

变更摘要:

  • 使表单按钮type="submit"
  • 使表单执行 POST
  • 删除所有 JavaScript 代码
  • 删除 jsLogin.php,并将相关代码从其中移动到 login.php 并包装在 if 中,该 <?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> 在尝试处理之前检查表单是否已提交。
  • 如果登录失败输出错误信息。如果成功,只需设置会话变量并重定向到 index.php

login.php:

npm install -g yo generator-code

附言还请注意我在上述评论中提出的各种安全要点。

,

你的 javascript 说

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

这意味着如果你回显 1 然后做一些事情。

您应该将 php 更改为 echo out 1 或将 javascript 更改为

if($.trim(data) === "You have successfully signed in!"){

重定向会起作用。

还有一件事—— 如果你这样做了,你的重定向会更好

setTimeout(function(){ document.location.replace("http://www.example.com"); },1000)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res