簡體   English   中英

HTML / PHP登錄重定向到php

[英]HTML/PHP login redirects to php

我有一個html頁面,該頁面使用php文件查詢數據庫,然后登錄用戶或拒絕訪問。 我遇到的問題是它只是重定向到php文件的url,而從不提供有關所發生情況的反饋。 這是我第一次使用html,php和mysql,因此請原諒我缺乏技術術語/知識。

HTML文件

<html><head><title> Sign-In </title></head>
<body>

<h1> Login Form </h1>
<div id="Sign-In">
<form method="POST" action="connectivity.php">
Username: <input type='text' name='username'> <br><br>
Password: <input type='password' name='password'> <br><br>
<input id="button" type="submit" name="submit" value="Log-In">

</form>
</div>
</body>
</html>

PHP文件

<?php
define('DB_HOST', 'localhost');
define('DB_NAME', 'project2');
define('DB_USER','root');
define('DB_PASSWORD','');

$con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error());
$db=mysql_select_db(DB_NAME,$con) or die("Failed to connect to MySQL: " . mysql_error());
/*
$ID = $_POST['user'];
$Password = $_POST['pass'];
*/
function SignIn()
{
session_start();   //starting the session for user profile page
if(!empty($_POST['user']))   //checking the 'user' name which is from Sign-In.html, is it empty or have some text
{
    $query = mysql_query("SELECT *  FROM customers where userName = '$_POST[user]' AND pass = '$_POST[pass]'") or die(mysql_error());
    $row = mysql_fetch_array($query) or die(mysql_error());
    if(!empty($row['userName']) AND !empty($row['pass']))
    {
        $_SESSION['userName'] = $row['pass'];
        echo "SUCCESSFULLY LOGIN TO USER PROFILE PAGE...";

    }
    else
    {
        echo "SORRY... YOU ENTERD WRONG ID AND PASSWORD... PLEASE RETRY...";
    }
}
}
if(isset($_POST['submit']))
{
    SignIn();
}

?>

嘗試這個:

<?php

if (!empty($row['userName']) AND !empty($row['pass'])) {

    $_SESSION['userName'] = $row['pass'];
    header("Location: mypage.php?is_login=1");
    exit;     // exit is must or you can use retun also
}

else {

    header("Location: mypage.php?is_login=0");
    exit;     // exit is must or you can use retun also             
}

?>

在頁面mypage.php上

<?php

if (isset($_GET['is_login']) ) {

    if ('1' == $_GET['is_login']) {

        echo "sucess";    
    }

    else {

        echo "failure";        
    }    
}
?>

在您的PHP文件中,嘗試重定向到先前的URL或新的HTML頁面(如Home Page):

header('Location: ' . $_SERVER['HTTP_REFERER']);

在你的:

if(isset($_POST['submit']))
{
    SignIn();
}

答案示例

使用header()或Javascript重定向到您的個人資料頁

header("Location: profilepage.php");                      //PHP
<script>window.location = 'profilepage.php'</script>      //JS

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM