表单提交后,在codeigner4应用程序中重新加载页面,重定向到url给定404中省略公共的页面.如何防止这种情况发生



我开发了一个codeigner4应用程序。我创建的登录页面被编码为在页面提交后重定向到自己,使用:

return $this->response->redirect(base_url('public/index.php/auth'));

页面重定向,但在表单提交后,当我重新加载页面时,浏览器会要求确认表单重新提交,单击"继续"后,页面会指向<PROJECTROOT>/index.php/auth而不是<PROJECTROOT>/public/index.php/auth,并给出404文件未找到错误。

公众是如何在这里被忽略的。如何正确地重新加载页面?

AuthController.php

<?php
namespace AppControllers;
use ConfigServices;
use AppModelsUserModel;
class Auth extends BaseController {

// login
public function index()
{

helper(['form']);

$session = session();

$email = $session->get('email');
if ($email) {

$password = $session->get('password');
$this->chooseLoginTo($email,$password,$session);
}
else if($this->request->getVar('email')){

$this->processLogin();
}
else {
$this->_showLoginForm();
}
}

public function processLogin()
{
helper(['form']);
//set rules validation form
$rules = [
'email'         => 'required|min_length[6]|max_length[50]|valid_email',
'password'      => 'required|min_length[6]|max_length[200]'
];
if($this->validate($rules)){
$this->_login();
}
else {
$this->_showLoginForm();
}
}

private function _showLoginForm()
{
$data['title'] = 'e-Ticketing Incident Reporting & Feedback IMS';
$data['validation'] = $this->validator;
//echo view('auth/login', $data);
echo view('sign-in',$data);
}
/*public function xss_clean($data){
$newdata=[];
foreach($data as $key=>$value){
$value = htmlentities($value, ENT_QUOTES | ENT_HTML5, 'UTF-8');
$newdata[$key]=$value;
}
return $newdata;
}*/
private function _login()
{
$session=session();
$request = service('request');
$email =$request->getVar('email');
$password=$request->getVar('password');
$this->chooseLoginTo($email,$password,$session);
}
public function chooseLoginTo($email,$password,$session){
helper('my_helper');
$model = new AppModelsUserModel();
$user = $model->where('email', $email)->first();
//print_r($user);exit();
if ($user) {
if ($user['is_active'] == 1) {
if (password_verify($password, $user['password'])) {
$data = [
'name' => $user['name'],
'email' => $user['email'],
'role_id' => $user['role_id'],
'logged_in' => TRUE
];

$data=xss_clean($data);

$session->set($data);
if ($user['role_id'] == 1) {

return $this->response->redirect(base_url('public/index.php/admin') );
} elseif($user['role_id'] == 3) {
return $this->response->redirect(base_url('public/index.php/report') );

}
else{

return $this->response->redirect(base_url('public/index.php/dashboard'));

}
}else{
$session->setFlashdata('msg', '<div class="bg-red-100 rounded-lg py-5 px-6 mb-4 text-base text-red-700 mb-3 error" role="alert">
Wrong Password!</div>');
return $this->response->redirect(base_url('public/index.php/auth') );
}
} else {
$session->setFlashdata('msg', '<div class="error alert-danger" role="alert">
This email has not been activated yet!</div>');

return $this->response->redirect(base_url('public/index.php/auth') );
}
} else {
$session->setFlashdata('msg', '<div class="error alert-danger" role="alert">
Email is not registered!</div>');

return $this->response->redirect(base_url('public/index.php/auth') );
}
}
}

查看:登录.php

<!--
=========================================================
* Soft UI Dashboard Tailwind - v1.0.4
=========================================================
* Product Page: https://www.creative-tim.com/product/soft-ui-dashboard-tailwind
* Copyright 2022 Creative Tim (https://www.creative-tim.com)
* Licensed under MIT (https://www.creative-tim.com/license)
* Coded by Creative Tim
=========================================================
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="apple-touch-icon" sizes="76x76" href="../assets/img/apple-icon.png" />
<link rel="icon" type="image/png" href="../assets/img/favicon.png" />
<title><?=$title?></title>
<!-- Fonts and icons -->
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700" rel="stylesheet" />
<!-- Font Awesome Icons -->
<script src="https://kit.fontawesome.com/42d5adcbca.js" crossorigin="anonymous"></script>

<!-- Nucleo Icons -->
<link href="../assets/css/nucleo-icons.css" rel="stylesheet" />
<link href="../assets/css/nucleo-svg.css" rel="stylesheet" />
<script src="https://cdn.tailwindcss.com"></script>
<!-- Main Styling -->
<link href="../assets/css/soft-ui-dashboard-tailwind.css?v=1.0.4" rel="stylesheet" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.0/jquery.validate.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.0/additional-methods.min.js"></script>
<style>
.errors {font-size:small;color:red;}
.error {font-size:small;color:red;}
.alert {font-size:small;color:red;}
</style>

</head>
<body class="m-0 font-sans antialiased font-normal bg-white text-start text-base leading-default text-slate-500">
<div class="container sticky top-0 z-sticky">
<div class="flex flex-wrap -mx-3">
<div class="w-full max-w-full px-3 flex-0">
<!-- Navbar -->
<nav class="absolute top-0 left-0 right-0 z-30 flex flex-wrap items-center px-4 py-2 mx-6 my-4 shadow-soft-2xl rounded-blur bg-white/80 backdrop-blur-2xl backdrop-saturate-200 lg:flex-nowrap lg:justify-start">
<div class="flex items-center justify-between w-full p-0 pl-6 mx-auto flex-wrap-inherit">
<!--<a class="py-2.375 text-xl mr-4 ml-4 whitespace-nowrap font-bold text-slate-700 lg:ml-0 animate__animated animate__heartBeat" href=""> <?=$title ?> </a>
-->
<h3 class="h4 text-gray-900 mb-4 "><?= $title; ?></h1>
<button navbar-trigger class="px-3 py-1 ml-2 leading-none transition-all bg-transparent border border-transparent border-solid rounded-lg shadow-none cursor-pointer text-lg ease-soft-in-out lg:hidden" type="button" aria-controls="navigation" aria-expanded="false" aria-label="Toggle navigation">
<span class="inline-block mt-2 align-middle bg-center bg-no-repeat bg-cover w-6 h-6 bg-none">
<span bar1 class="w-5.5 rounded-xs relative my-0 mx-auto block h-px bg-gray-600 transition-all duration-300"></span>
<span bar2 class="w-5.5 rounded-xs mt-1.75 relative my-0 mx-auto block h-px bg-gray-600 transition-all duration-300"></span>
<span bar3 class="w-5.5 rounded-xs mt-1.75 relative my-0 mx-auto block h-px bg-gray-600 transition-all duration-300"></span>
</span>
</button>
<div navbar-menu class="items-center flex-grow overflow-hidden transition-all duration-500 ease-soft lg-max:max-h-0 basis-full lg:flex lg:basis-auto">
<ul class="flex flex-col pl-0 mx-auto mb-0 list-none lg:flex-row xl:ml-auto">
<!--<li>
<a class="flex items-center px-4 py-2 mr-2 font-normal transition-all lg-max:opacity-0 duration-250 ease-soft-in-out text-sm text-slate-700 lg:px-2" aria-current="page" href="../pages/dashboard.html">
<i class="mr-1 fa fa-chart-pie opacity-60"></i>
Dashboard
</a>
</li>
<li>
<a class="block px-4 py-2 mr-2 font-normal transition-all lg-max:opacity-0 duration-250 ease-soft-in-out text-sm text-slate-700 lg:px-2" href="../pages/profile.html">
<i class="mr-1 fa fa-user opacity-60"></i>
Profile
</a>
</li>
<li>
<a class="block px-4 py-2 mr-2 font-normal transition-all lg-max:opacity-0 duration-250 ease-soft-in-out text-sm text-slate-700 lg:px-2" href="../pages/sign-up.html">
<i class="mr-1 fas fa-user-circle opacity-60"></i>
Sign Up
</a>
</li>
<li>
<a class="block px-4 py-2 mr-2 font-normal transition-all lg-max:opacity-0 duration-250 ease-soft-in-out text-sm text-slate-700 lg:px-2" href="../pages/sign-in.html">
<i class="mr-1 fas fa-key opacity-60"></i>
Sign In
</a>
</li>-->
</ul> 
<!-- online builder btn  -->
<!--<li class="flex items-center">
<a
class="leading-pro ease-soft-in text-fuchsia-500 border-fuchsia-500 text-xs tracking-tight-soft bg-150 bg-x-25 rounded-3.5xl hover:border-fuchsia-500 hover:scale-102 hover:text-fuchsia-500 active:hover:border-fuchsia-500 active:hover:scale-102 active:hover:text-fuchsia-500 active:opacity-85 active:shadow-soft-xs active:bg-fuchsia-500 active:border-fuchsia-500 mr-2 mb-0 inline-block cursor-pointer border border-solid bg-transparent py-2 px-8 text-center align-middle font-bold uppercase shadow-none transition-all hover:bg-transparent hover:opacity-75 hover:shadow-none active:scale-100 active:text-white active:hover:bg-transparent active:hover:opacity-75 active:hover:shadow-none"
target="_blank"
href="https://www.creative-tim.com/builder/soft-ui?ref=navbar-dashboard&amp;_ga=2.76518741.1192788655.1647724933-1242940210.1644448053"
>Online Builder</a
>
</li> -->
<ul class="hidden pl-0 mb-0 list-none lg:block lg:flex-row">
<li>
<a href="<?= base_url('public/index.php/auth/registration')?>" target="_blank" class="leading-pro hover:scale-102 hover:shadow-soft-xs active:opacity-85 ease-soft-in text-xs tracking-tight-soft shadow-soft-md bg-150 bg-x-25 bg-gradient-to-tl from-gray-900 to-slate-800 rounded-3.5xl mb-0 mr-1 inline-block cursor-pointer border-0 bg-transparent px-8 py-2 text-center align-middle font-bold uppercase text-white transition-all">Client Registration</a>
</li>
</ul>
</div> 
</div>
</nav>
</div>
</div>
</div>


<?=form_open(base_url('public/index.php/auth'));
?>
<main class="mt-0 transition-all duration-200 ease-soft-in-out">
<section>
<div class="relative flex items-center p-0 overflow-hidden bg-center bg-cover min-h-75-screen">
<div class="container z-10">
<div class="flex flex-wrap mt-0 -mx-3">
<div class="flex flex-col w-full max-w-full px-3 mx-auto md:flex-0 shrink-0 md:w-6/12 lg:w-5/12 xl:w-4/12">
<div class="relative flex flex-col min-w-0 mt-32 break-words bg-transparent border-0 shadow-none rounded-2xl bg-clip-border">
<div class="p-6 pb-0 mb-0 bg-transparent border-b-0 rounded-t-2xl">
<h3 class="relative z-10 font-bold text-transparent bg-gradient-to-tl from-blue-600 to-cyan-400 bg-clip-text">Sign In Page</h3>
<p class="mb-0">Enter your email and password to sign in</p>
</div>
<!--<div class="bg-red-100 rounded-lg py-5 px-6 mb-4 text-base text-red-700 mb-3 error" role="alert">
-->
<?= ConfigServices::validation()->listErrors(); ?>
<!-- </div> -->

<span class="d-none alert alert-success mb-3" id="res_message"></span>
<!--<div class="bg-red-100 rounded-lg py-5 px-6 mb-4 text-base text-red-700 mb-3" role="alert">
-->
<?php if(session()->getFlashdata('msg')):?>
<?= session()->getFlashdata('msg') ?>

<?php endif;
unset($_SESSION['msg']);
?>
<!--</div>-->
<div class="flex-auto p-6">
<form role="form">
<label class="mb-2 ml-1 font-bold text-xs text-slate-700">Email</label>
<div class="mb-4">
<input type="email" name="email" class="focus:shadow-soft-primary-outline text-sm leading-5.6 ease-soft block w-full appearance-none rounded-lg border border-solid border-gray-300 bg-white bg-clip-padding px-3 py-2 font-normal text-gray-700 transition-all focus:border-fuchsia-300 focus:outline-none focus:transition-shadow" placeholder="Email" aria-label="Email" aria-describedby="email-addon" />
</div>
<label class="mb-2 ml-1 font-bold text-xs text-slate-700">Password</label>
<div class="mb-4">
<input type="password" name="password" class="focus:shadow-soft-primary-outline text-sm leading-5.6 ease-soft block w-full appearance-none rounded-lg border border-solid border-gray-300 bg-white bg-clip-padding px-3 py-2 font-normal text-gray-700 transition-all focus:border-fuchsia-300 focus:outline-none focus:transition-shadow" placeholder="Password" aria-label="Password" aria-describedby="password-addon" />
</div>
<!--<div class="min-h-6 mb-0.5 block pl-12">
<input id="rememberMe" class="mt-0.54 rounded-10 duration-250 ease-soft-in-out after:rounded-circle after:shadow-soft-2xl after:duration-250 checked:after:translate-x-5.25 h-5 relative float-left -ml-12 w-10 cursor-pointer appearance-none border border-solid border-gray-200 bg-slate-800/10 bg-none bg-contain bg-left bg-no-repeat align-top transition-all after:absolute after:top-px after:h-4 after:w-4 after:translate-x-px after:bg-white after:content-[''] checked:border-slate-800/95 checked:bg-slate-800/95 checked:bg-none checked:bg-right" type="checkbox" checked="" />
<label class="mb-2 ml-1 font-normal cursor-pointer select-none text-sm text-slate-700" for="rememberMe">Remember me</label>
</div>-->
<div class="text-center">
<!--<button type="submit" class="inline-block w-full px-6 py-3 mt-6 mb-0 font-bold text-center text-white uppercase align-middle transition-all bg-transparent border-0 rounded-lg cursor-pointer shadow-soft-md bg-x-25 bg-150 leading-pro text-xs ease-soft-in tracking-tight-soft bg-gradient-to-tl from-blue-600 to-cyan-400 hover:scale-102 hover:shadow-soft-xs active:opacity-85">Sign in</button>-->
<input type="submit" name="submit" value="Sign In" class="inline-block w-full px-6 py-3 mt-6 mb-0 font-bold text-center text-white uppercase align-middle transition-all bg-transparent border-0 rounded-lg cursor-pointer shadow-soft-md bg-x-25 bg-150 leading-pro text-xs ease-soft-in tracking-tight-soft bg-gradient-to-tl from-blue-600 to-cyan-400 hover:scale-102 hover:shadow-soft-xs active:opacity-85" />  
</div>
</form>
</div>
<!--
<div class="p-6 px-1 pt-0 text-center bg-transparent border-t-0 border-t-solid rounded-b-2xl lg:px-2">
<p class="mx-auto mb-6 leading-normal text-sm">
Don't have an account?
<a href="../pages/sign-up.html" class="relative z-10 font-semibold text-transparent bg-gradient-to-tl from-blue-600 to-cyan-400 bg-clip-text">Sign up</a>
</p>
</div> -->
</div>
</div>
<div class="w-full max-w-full px-3 lg:flex-0 shrink-0 md:w-6/12">
<div class="absolute top-0 hidden w-3/5 h-full -mr-32 overflow-hidden -skew-x-10 -right-40 rounded-bl-xl md:block">
<div class="absolute inset-x-0 top-0 z-0 h-full -ml-16 bg-cover skew-x-10" style="background-image: url('<?=base_url('public')?>/assets/img/curved-images/curved6.jpg')"></div>
</div>
</div>
</div>
</div>
</div>
</section>
</main>
<footer class="py-12">
<div class="container">
<!--<div class="flex flex-wrap -mx-3">
<div class="flex-shrink-0 w-full max-w-full mx-auto mb-6 text-center lg:flex-0 lg:w-8/12">
<a href="javascript:;" target="_blank" class="mb-2 mr-4 text-slate-400 sm:mb-0 xl:mr-12"> Company </a>
<a href="javascript:;" target="_blank" class="mb-2 mr-4 text-slate-400 sm:mb-0 xl:mr-12"> About Us </a>
<a href="javascript:;" target="_blank" class="mb-2 mr-4 text-slate-400 sm:mb-0 xl:mr-12"> Team </a>
<a href="javascript:;" target="_blank" class="mb-2 mr-4 text-slate-400 sm:mb-0 xl:mr-12"> Products </a>
<a href="javascript:;" target="_blank" class="mb-2 mr-4 text-slate-400 sm:mb-0 xl:mr-12"> Blog </a>
<a href="javascript:;" target="_blank" class="mb-2 mr-4 text-slate-400 sm:mb-0 xl:mr-12"> Pricing </a>
</div>
<div class="flex-shrink-0 w-full max-w-full mx-auto mt-2 mb-6 text-center lg:flex-0 lg:w-8/12">
<a href="javascript:;" target="_blank" class="mr-6 text-slate-400">
<span class="text-lg fab fa-dribbble"></span>
</a>
<a href="javascript:;" target="_blank" class="mr-6 text-slate-400">
<span class="text-lg fab fa-twitter"></span>
</a>
<a href="javascript:;" target="_blank" class="mr-6 text-slate-400">
<span class="text-lg fab fa-instagram"></span>
</a>
<a href="javascript:;" target="_blank" class="mr-6 text-slate-400">
<span class="text-lg fab fa-pinterest"></span>
</a>
<a href="javascript:;" target="_blank" class="mr-6 text-slate-400">
<span class="text-lg fab fa-github"></span>
</a>
</div>
</div> -->
<div class="flex flex-wrap -mx-3">
<div class="w-8/12 max-w-full px-3 mx-auto mt-1 text-center flex-0">
<p class="mb-0 text-slate-400">
Copyright ©
<script>
document.write(new Date().getFullYear());
</script>
<?=$title;?> C-DIT
</p>
</div>
</div>
</div>
</footer>
</body>
<script>
if ($("#login").length > 0) {
$("#login").validate({
rules: {
password: {
required: true,
},
email: {
required: true,
maxlength: 50,
email: true,
},
},
messages: {
password: {
required: "Please enter password",
},
email: {
required: "Please enter valid email",
email: "Please enter valid email",
maxlength: "The email name should less than or equal to 50 characters",
},
},
})
}
</script> 
<!-- plugin for scrollbar  -->
<script src="../assets/js/plugins/perfect-scrollbar.min.js" async></script>
<!-- main script file  -->
<script src="../assets/js/soft-ui-dashboard-tailwind.js?v=1.0.4" async></script>
</html>

.htaccess

<IfModule authz_core_module>
Require all denied
</IfModule>
<IfModule !authz_core_module>
Deny from all
</IfModule>

Routes.php

(app/Config/Routes.php(

$routes->get('/auth', 'Auth::index');
$routes->post('/auth', 'Auth::index'); 

(AuthController.php(

而不是:❌

return $this->response->redirect(base_url('public/index.php/auth'));

使用此:✅

return redirect()->back()->withInput();

资源:

old($key[, $default = null[, $escape = 'html']])

redirect(string $route)

附录1

确保您的public/.htaccess文件类似于:

CodeIgniter4/public/.htaccess

附录2

(视图:sign-in.php(

替换:

<?=form_open(base_url('public/index.php/auth'));
?>

带有:

<?= echo form_open('/auth'); ?>

您的HTML标记非常混乱。您有一个form_open(...),但没有相应的form_close(...)声明。

此外,您似乎有嵌套的<form>标记,这是没有意义的。不能嵌套<form>标记。你能嵌套HTML表单吗?

您的内部<form>标记还幸运地获得了action="..."属性。

附录3

在您的控制器中,您似乎有许多Redirect路由,其形式为:return $this->response->redirect(base_url('public/index.php/...')。如果尚未在app/Config/Routes.php中设置显式用户定义路由,请执行此操作。此外,在控制器中,将所有Redirect路由替换为类似的内容:return redirect()->to('/admin');

附录4

(app/Config/Routes.php(

请确保已禁用自动路由。I.e:

$routes->setAutoRoute(false);

相关内容

最新更新