2016-05-03 48 views
0

所以我想为我的web开发类的最终项目做一个有效的登录。每当我尝试使用正确的凭证登录时,我总是被重定向到“something.php”页面,并且我从来没有收到任何输入到我的代码中的输出。这种情况甚至发生在我输入有效的登录凭据时。我知道这不是一种安全的登录方式,但这只是用于最终项目目的。我附上了所有我的文件(减去图片),尽管您可能不需要所有这些文件。最终项目的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> 

回答

0

在行

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

您阅读了一个名为'action'的POST表单域。我无法在任何网络表单中找到该字段...也许给您的SUBMIT按钮起一个名称即可取而代之。 Action是Web表单的一个属性,而不是字段名称。

0

它看起来像它忽略你的jQuery登录的代码,因为当你点击Login您的形式提交,并因为你检查是否$_POST['action']是空

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

,因为你没有任何形式的失败具有该名称的元素。 formaction属性仅指定将数据发送到哪个页面。

我想你可能会尝试加onsubmit="return false"到你的form为了让它只使用你的$.post方法。

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

,如果你修改你打开变量赋值

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用户定义的数据从其他页面上的表单。

我希望这会有所帮助。