从php脚本

时间:2017-09-04 15:16:17

标签: php html redirect

我正在慢慢开发这个网站,我已经坚持了一段时间。

以下是登录和注册页面的代码(还有一些其他文件,但我并不认为他们需要获得这个想法)



<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>
&#13;
&#13;
&#13;

所以当你登录这个php脚本时运行...

&#13;
&#13;
<?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?';
}
?>
&#13;
&#13;
&#13;

任何帮助都会很棒:)

我试图在if语句中添加一个简单的PHP重定向

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

但没有任何反应,没有错误。我也尝试使用echo将它带到页面,因为PHP脚本在另一个文件中并且没有运气。

更新1

按照建议尝试了这一点,但点击登录后没有运气,2个用户名和密码字段变为空白,没有任何反应 - 没有错误。

&#13;
&#13;
<?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?';
}
?>
&#13;
&#13;
&#13;

更新2

注释掉include pdo_connect会导致此错误,因为它与sql数据库的密码如何对话...

error

更新3

尝试捕捉没有任何更改/发生且没有错误消息。

&#13;
&#13;
<?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)
{
	echo "something went wrong";
}
?>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

George你可以使用标题函数重定向路径,在你的情况下放置它header("location: main.html");

答案 1 :(得分:0)

即使我过去也不喜欢标题,因此我只是使用它。

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

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