[英]How redirect php Window.location.href or Window.open () methods in php and JavaScript
I follow up a tutorial to learn more about php, in it's source code there is something which seems works at that time but not anymore. 我跟进了一个教程,以了解有关php的更多信息,在它的源代码中,有些东西在当时似乎可行,但现在不再可用。 here is the code , please let me know what should i change in the code in order to make login process work (currently after entering a valid user name and pass and clicking login it freezes and show first page and not go to home.php
这是代码,请让我知道我应该在代码中进行哪些更改才能使登录过程正常运行(当前,在输入有效的用户名并通过并单击登录后,它冻结并显示第一页,而不是转到home.php
here is template/header.php: 这是template / header.php:
<div class="container">
<!--Head wrap starts-->
<div id="head_wrap">
<!--Header starts-->
<div id="header">
<img src="images/logo.png" style="float:left;"/>
<form method="post" action="" id="form1">
<strong>Email:</strong>
<input type="email" id="email" name="email" placeholder="Email" required="required" />
<strong>Password:</strong>
<input type="password" id="pass" name="pass" placeholder="****" required="required"/>
<button type="submit" id="login">Login</button>
</form>
</div>
<!--Header ends-->
</div>
here is login.php 这是login.php
<?php
session_start();
include("includes/connection.php");
if(isset($_POST['login'])){
$email = mysqli_real_escape_string($con,$_POST['email']);
$pass = mysqli_real_escape_string($con,$_POST['pass']);
$get_user = "select * from users where user_email='$email' AND user_pass='$pass'";
$run_user = mysqli_query($con,$get_user);
$check = mysqli_num_rows($run_user);
if($check==1){
$email = mysqli_real_escape_string($con,$_POST['email']);
$_SESSION['user_email']=$email;
echo "<script>window.open('home.php','_self')</script>";
}
else {
echo "<script>alert('Passowrd or email is not correct!')</script>";
}
}
?>
please note i have tried 请注意,我已经尝试过
echo "<script> window.location.href = 'home.php';</script>";
instead of 代替
echo "<script>window.open('home.php','_self')</script>";
and still doesn't work, since it's tutorial and i have search through stackoverflow can't find any answer i appreciate your help. 仍然不起作用,因为它是本教程,而且我通过stackoverflow搜索无法找到任何答案,我感谢您的帮助。
You have to use header(...) function but don't forget that your page keep to run at the end. 您必须使用header(...)函数,但不要忘记页面始终在最后运行。 Don't forget to use with
die
to stop your script. 不要忘记与
die
一起使用来停止脚本。 ;) ;)
die(header("Location: home.php"))
or after 5 seconds : 或5秒后:
header("refresh: 5; url=home.php");
This is your HTML code but with a submit button. 这是您的HTML代码,但是带有一个提交按钮。 You say all files are located in the same folder so this should work.
您说所有文件都位于同一文件夹中,因此应该可以使用。 I did not make any changes to login.php but it should run when the page is submitted.
我没有对login.php进行任何更改,但应在提交页面时运行。
<div class="container">
<!--Head wrap starts-->
<div id="head_wrap">
<!--Header starts-->
<div id="header">
<img src="images/logo.png" style="float:left;"/>
<form method="post" action="login.php" id="form1">
<strong>Email:</strong>
<input type="email" id="email" name="email" placeholder="Email" required="required" />
<strong>Password:</strong>
<input type="password" id="pass" name="pass" placeholder="****" required="required"/>
<input type="submit" id="login" name="login" value="Login">
</form>
</div>
<!--Header ends-->
</div>
</div>
Edit: I can't debug your entire project but after looking over some things I see you are not using the 'name' attribute. 编辑:我无法调试您的整个项目,但查看了一些内容后,我发现您没有使用'name'属性。 When a page is submitted a name/value pair is sent in the $_POST array.
提交页面后,将在$ _POST数组中发送一个名称/值对。 If you have no 'name' attribute nothing is sent.
如果没有“名称”属性,则不会发送任何内容。 Start by adding the 'name' attribute.
首先添加“名称”属性。 I have modified the above HTML code to show you how.
我已经修改了上面的HTML代码,向您展示了如何。
if($check==1){
$email = mysqli_real_escape_string($con,$_POST['email']);
$_SESSION['user_email']=$email;
return 1;
}
else {
return 0;
}
and javascript check status 1 and 0 then window.location.href and window.open use 和javascript检查状态1和0,然后window.location.href和window.open使用
Check in your file.. 签入文件。
1) header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP
1)必须先通过常规HTML标记,文件中的空白行或从PHP发送任何实际输出,然后再调用header()
2) Combine all your PHP codes and make sure you don't have any spaces at the beginning of the file.
2)合并所有PHP代码,并确保文件开头没有空格。
3) after header('location: home.php');
3)在header('location:home.php')之后; add exit();
添加exit();
4) after sesssion_start() add ob_start();
4)在sesssion_start()之后添加ob_start();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.