2017-09-04 149 views
-1

我正在慢慢开发这个网站,而且我一直在这个问题上停留了一段时间。从一个PHP脚本重定向到另一个页面

这里是在符号的代码和注册页面(有一些,但我不认为他们需要得到的想法的几个其他文件)

<html> 
 
<head> 
 
    <h1>Welcome to <b>SPICY MEMES</b>!</h1> 
 
    <h3>Your favourite meme network. </h3> 
 
    <p>Please login to view all the spiciest memes on the world wide of webz:</p> 
 
    <meta charset="utf-8"> 
 
    <title>Spicy memes</title> 
 
    <style type="text/css"> 
 
     .form { 
 
      width: 30%; 
 
     } 
 
     input[type="text"], input[type="password"] { 
 
      width: 99%; 
 
      height: 1.5em; 
 
      padding-bottom: 5px; 
 
      margin-bottom: 4px; 
 
     } 
 
    </style> 
 
</head> 
 
<body> 
 
    
 
    <div class="form"> 
 
     <fieldset> 
 
      <legend>&nbsp;LOGIN&nbsp;</legend> 
 
      <form name="login" action="inc/php/login.php" method="post"> 
 
       <input name="uname" type="text" placeholder="please enter your user name"><br /> 
 
       <input name="upassword" type="password" placeholder="please enter your password or passphrase"><br /> 
 
       <input name="submit" type="submit" value="log in"/> 
 
      </form> 
 
     </fieldset> 
 
    </div> 
 
    <p>or alternatively, create an account</p> 
 
    <div class="form"> 
 
     <fieldset> 
 
      <legend>&nbsp;REGISTER&nbsp;</legend> 
 
      <form name="register" action="inc/php/register.php" method="post"> 
 
       <input name="fname" type="text" placeholder="please enter your first name" required><br /> 
 
       <input name="lname" type="text" placeholder="please enter your last name" required><br /> 
 
       <input name="uname" type="text" placeholder="please enter a user name" required><br /> 
 
       <input name="upassword" type="password" placeholder="please enter a password or passphrase" required><br /> 
 
       <input name="submit" type="submit" value="register" /> 
 
      </form> 
 
     </fieldset> 
 
    </div> 
 
    <div id="response"></div> 
 
    <script type="text/javascript" src="inc/jquery/jquery-1.11.2.min.js"></script> 
 
    <script type="text/javascript" src="inc/jquery/functions.js"></script> 
 
</body> 
 
</html>

所以,当你在这个PHP脚本登录运行...

<?php 
 
include 'pdo_connect.php'; 
 

 
if(!empty($_POST)) { 
 
    $query = "SELECT `password` FROM `users` WHERE `uname` = ?"; 
 
    $params = array($_POST['uname']); 
 
    $results = dataQuery($query, $params); 
 
} 
 

 
$hash = $results[0]['password']; // first and only row if username exists; 
 
$loggedin = 0; 
 
if (password_verify($_POST['upassword'], $hash)) 
 
{ 
 
    //here i want to redirect the login page to a page called main.html 
 
} 
 
else 
 
{ 
 
    echo 'Invalid username/password, are you registered?'; 
 
} 
 
?>

任何帮助将是巨大的:)

我已尝试添加一个简单的PHP if语句

header("Location: http://example.com/myOtherPage.php"); 
die(); 

但没有任何反应,并没有错误重定向。由于PHP脚本在另一个文件中,并且没有运气,我也尝试使用echo将其传递到页面。

更新1

尝试这样的建议,但登录后没有运气点击了2个和密码字段变为空白,什么也没有发生 - 没有错误。

<?php 
 

 
include 'pdo_connect.php'; 
 
var_dump($_POST); 
 
if(!empty($_POST)) { 
 
    $query = "SELECT `password` FROM `users` WHERE `uname` = ?"; 
 
    $params = array($_POST['uname']); 
 
    $results = dataQuery($query, $params); 
 
} 
 

 
$hash = $results[0]['password']; // first and only row if username exists; 
 
$loggedin = 0; 
 
if (password_verify($_POST['upassword'], $hash)) 
 
{ 
 
     header("Location: main.html"); 
 
     exit; 
 
} 
 
else 
 
{ 
 
    echo 'Invalid username/password, are you registered?'; 
 
} 
 
?>

更新2

注释掉include pdo_connect导致这个错误,因为它怎么会谈到的密码,SQL数据库...

error

更新3

与尝试没有任何更改/发生并没有错误消息。

<?php 
 

 
include 'pdo_connect.php'; 
 
var_dump($_POST); 
 
if(!empty($_POST)) { 
 
    $query = "SELECT `password` FROM `users` WHERE `uname` = ?"; 
 
    $params = array($_POST['uname']); 
 
    $results = dataQuery($query, $params); 
 
} 
 

 
$hash = $results[0]['password']; // first and only row if username exists; 
 
$loggedin = 0; 
 

 
try 
 
{ 
 
    if (password_verify($_POST['upassword'], $hash)) 
 
    { 
 
     header("Location: main.html"); 
 
     exit; 
 
    } 
 

 
    else 
 
    { 
 
    echo 'Invalid username/password, are you registered?'; 
 
    } 
 
} 
 
catch(Exception $e) 
 
{ 
 
\t echo "something went wrong"; 
 
} 
 
?>

+0

可能的重复[如何在PHP中进行重定向?](https://stackoverflow.com/questions/768431/how-to-make-a-redirect-in-php) – Jeff

+0

嘿,是的,我看了在那,并尝试它,并没有工作,没有任何反应。我也尝试使用回声发送到主页面,但再次没有错误,但没有任何反应。 –

+0

是你把这个路径放在这个路径上的PHP代码“inc/php/login.php”?,请在'if(!empty($ _ POST))之前做一个'var_dump($ _ POST)''并且分享你的结果。 – Jeff

回答

0

即使我有HADE问题,在过去的头部,因此我只是用这个。

echo "<script>window.location.replace('mypage.php')</script>"; 

当然mypage.php可以是您选择的任何网址,但不要在网址周围加双引号。

0

乔治,你可以使用重定向功能头的路径,你的情况的地方。它header("location: main.html");

+0

我试过,但没有发生:( –

相关问题