最终项目的php登录帮助


php login help for final project

所以我正在尝试为我的web开发类的最终项目进行有效登录。每当我尝试使用正确的凭据登录时,我总是被重定向到"something.php"页面,并且我从未收到任何我放入代码中的输出。甚至当我输入有效的登录凭据时也会发生这种情况。我知道这不是一种安全的登录方式,但这只是为了最终项目的目的。我已经在下面附上了我所有的文件(不包括图片),尽管你可能不需要所有的文件。

something.php文件(用于登录的php)

<?php
$action = empty($_POST['action']) ? false : $_POST['action'];
/*var radio = document.getElementById('radio');*/
switch ($action) {
    case 'login':
        $username = empty($_POST['username']) ? '' : $_POST['username'];
        $password = empty($_POST['password']) ? '' : $_POST['password'];
        if ($username=='test' && $password=='pass') {
            setcookie('userid', $username);
            $response = 'Login: Sucess';
        }
        else {
            $response = 'Login: Fail';
        }
        print $response;
        break;
    case 'get':
        $userid = empty($_COOKIE['userid']) ? '' : $_COOKIE['userid'];
        if ($userid=='test') {
            $response = 'Todays special are organic Brazilian strawberries $1.75 a pound';
        }
        if ($username!='test' || $password!='pass'){ 
            header('Location: XXX/something.php');
            echo "username and password combo are not valid";
            //radio.value = "logged out";
        }
        print $response;
        break;        
    case 'logout':
        setcookie('userid', '', 1);
        print 'Logged out';
        break;
}
?>

login.html页面

<div class="group">
<div id="radio">
<form id="radio" action="">
  <input type="radio" name="select" value="login"> Logged In<br>
  <input type="radio" name="select" value="logout" checked> Logged Out<br>
</form>
</div>
<form id="content2" class="itemBlock">
<p> Don't have an account? Sign up here!</p>
  First name:<br>
  <input type="text" name="firstname"><br>
  Last name:<br>
  <input type="text" name="lastname"><br>
  E-mail: <br>
  <input type="text" name="email"><br>
  Password: <br>
  <input type="text" name="password"><br>
    <br>
     <input type="button" value="Register" onclick="addToCartFxn()">
</form>
<div id="center">
<form id="content" class="itemBlock" action="something.php" method="post">
    <br> <br>
  E-mail: <br>
  <input type="text" name="email"><br>
  Password: <br>
  <input type="password" name="password"><br>
    <input type="submit" class="get" value="Login" id="login">
    <input type="submit" value="Logout" id="logout">
</form>
    </div>
</div>

final.php页面

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>jQuery UI Tabs - Content via Ajax</title>
  <link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
  <script src="http://code.jquery.com/jquery-1.10.2.js"></script>
  <script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
 <!--<link rel="stylesheet" href="/resources/demos/style.css">-->
<!--<script src="jquery-1.10.2.min.js"></script>-->
  <script>
  $(function() {
    $( "#tabs" ).tabs({
      beforeLoad: function( event, ui ) {
        ui.jqXHR.fail(function() {
          ui.panel.html(
            "Couldn't load this tab. We'll try to fix this as soon as possible. ");
        });
      }
    });
  });

    $(function(){
        $('#login').click(function(){
            $.post('something.php', 
            {
                action: 'login',
                username: $('#username').val(),
                password: $('#password').val()
            },
            function(data){
                $('#center').html(data);
            });
        });
        $('#logout').click(function(){
            $.post('something.php',
            {
                action: 'logout'
            },
            function(data){
                $('#center').html(data);
            });
        });
        $('.get').click(function(){
            $.post('something.php',
            {
                action: 'get'
            },
            function(data){
                $('#center').html(data);
            });
        });
    }); 
    function addToCartFxn() {
        alert("This feature is coming soon!");
    }
  </script>
    <style>
        #floatright {
            float:right;
            }

        #header{
            background-image: url("tree rocks header.jpg");
            width: 100%;
            height: 200px;
            padding-top: 1px;
            text-align: center;
            background-size: cover;
            background-position-y: 3255px;
            background-position-x: -2112px;
        }
        #headertext {
          z-index: 100;
          color: white;
          font-size: 72px;
          font-family: exo, arial, serif;
        }

        @font-face {
            /* Declare the name of the font (we make this value up) */
            font-family: exo;
            /* And where it's located */
            src: url("Exo-Medium.otf");
        }

            .addtocart{
            background-color: #4CAF50; /* Green */
            border: none;
            color: white;
            padding: 7px 12px;
            border-radius: 7px;
            text-align: center;
            text-decoration: none;
            font-size: 5px;
            margin-bottom: 3px;
            }
        #radio {
        float: right;    
        }
        #content {
            font-family: exo, arial, serif;
            text-align: center;
            border-radius: 25px;
            background-color:forestgreen;
            align-content: center;
        }
        #content2 {
            font-family: exo, arial, serif;
            float:left;
        }
        #logout{
            margin: 5px;
        }
        #login{
            margin: 5px;
        }
        .itemBlock{
            display:inline-block;
            margin: 10px;
            border: 2px black;
            border-style: solid;
            text-align: left;
            padding-left: 5px;
            padding-right: 5px;
    }
        @font-face {
            /* Declare the name of the font (we make this value up) */
            font-family: exo;
            /* And where it's located */
            src: url("Exo-Medium.otf");
        }    
        #center{
            margin-left: 42%;
        }

        body {
           min-height: 100%;
            height: auto! important;
        }
        .group:after {
            content: "";
            display: table;
            clear: both;
        }
    </style>
    </head>
<body>
<div id="header"> 
    <p id="headertext"> Claw's Cache</p> 
    </div>
<div id="tabs">
  <ul>
    <li><a href="#tabs-1"> Our Products </a></li>
    <li><a href="#tabs-2"> Available Produce </a></li>
      <li id="floatright"> <a href="login.html"> Login</a></li><!--tabs-3's content is being put there by ajax-->
  </ul>
  <div id="tabs-1">
      <p>Listed here are all the current vendors that are associated with us products.</p>
<?php
//Use glob function to get the files
//Note that we have used " * " inside this function. If you want to get only JPEG or PNG use
//below line and commnent $images variable currently in use
$images = glob("*.png");
$i=0;
//Display image using foreach loop
foreach($images as $image){
echo '<div class="itemBlock"> <a href="'.$image.'" target="_blank"><img src="'.$image.'" height="250" width="200" /></a> <br>';
echo '<button type="button" class="addtocart" onclick="addToCartFxn()"> add to cart</button> </div>';    
}
?>           
  </div>
  <div id="tabs-2">
<p>Our available produce page is being updated currently! 
<br> <br> As a placeholder, we have attached a video that many of our customers recommend to every person interested in indoor gardening.</p><br>
        <iframe width="560" height="315" src="https://www.youtube.com/embed/RWCIaydwM_w" frameborder="0" allowfullscreen></iframe>
  </div>
  </div>

</body>
</html>

在线上

$action = empty($_POST['action']) ? false : $_POST['action'];

您阅读了一个名为"action"的POSTed表单字段。我在你的任何网页表单中都找不到该字段。。。也许给你的"提交"按钮起个名字吧。操作是web表单的一个元素,而不是字段名。

它似乎忽略了您的jquery登录代码,因为当您单击Login时您的表单正在提交,并且您正在检查$_POST['action']是否为空

$action = empty($_POST['action']) ? false : $_POST['action'];

它失败了,因为您没有任何具有该名称的表单元素。CCD_ 3的CCD_。

我认为您可以尝试将onsubmit="return false"添加到form中,以使其仅使用$.post方法。

<form onsubmit="return false" id="content" class="itemBlock" action="something.php" method="post">

如果您将开场变量赋值修改为,会发生什么

empty($_POST['action']) ? $action = false : $action = $_POST['action'];

我不能100%确定为什么会发生这种情况,但我从过去的经验中知道,缩写IF语句可能会产生一些意想不到的结果。

除此之外,您似乎没有在任何地方设置$_POST['action']字段?

如果您引用的是打开<form>标签中指定的传输方法,那么这是错误的。

如果您的简写IF语句用于确定数据是如何传递到something.php文件中的,那么您可以执行

empty($_POST) ? (empty($_GET) ? echo "No User Data Passed" : $data = $_GET;) : $data = $_POST;

(我知道这看起来很冗长,但可以省略对$_GET数据的额外检查,它只是作为概念验证类型检查)。

由此,$data变量将使用其他页面上表单中的$_GET或$_POST用户定义数据进行设置。

我希望这能有所帮助。