2016-05-03 5 views
0

Also ich versuche, eine gültige Anmeldung für meine Web-dev-Klasse 'Abschlussprojekt zu machen. Immer wenn ich versuche, mich mit den richtigen Zugangsdaten anzumelden, werde ich immer auf die Seite "something.php" weitergeleitet, und ich bekomme nie die Ausgabe, die ich in meinen Code eingegeben habe. Dies geschieht sogar, wenn ich gültige Anmeldedaten eingib.Ich weiß, dass dies keine sichere Möglichkeit ist, eine Anmeldung durchzuführen, aber dies ist nur für endgültige Projektzwecke. Ich habe alle meine Dateien unten angehängt (minus Bilder), obwohl Sie wahrscheinlich nicht alle von ihnen brauchen.php login hilfe für das endgültige projekt

Die something.php-Datei (die PHP für die Login)

<?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 Seite

<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 Seite

<!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> 

Antwort

0

In Zeile

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

Sie haben ein POST-Formularfeld namens "action" gelesen. Ich kann dieses Feld in keinem Ihrer Webformulare finden ... Vielleicht geben Sie Ihren SUBMIT-Buttons einen Namen, die stattdessen retrive sind. Aktion ist ein Attribut des Webformulars, kein Feldname.

0

es sieht aus wie es ist Ihre jquery Login-Code zu ignorieren, weil Ihr Formular aus, wenn Sie Login klicken, und da Sie überprüfen, ob $_POST['action'] leer

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

es schlägt fehl, weil Sie keine Form haben Element mit diesem Namen. Das formaction Attribut gibt nur an, auf welcher Seite die Daten gesendet werden.

Ich denke, Sie könnten versuchen, onsubmit="return false" zu Ihrem form hinzufügen, um es nur Ihre $.post Methode zu verwenden.

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

Was passiert, wenn Sie Ihre Öffnungsvariablenzuweisung ändern

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

Ich bin nicht 100% sicher, warum dies geschieht, aber ich weiß aus Erfahrung, dass die Stenografie IF Aussagen können einige unerwartete geben Ergebnisse.

Obendrein sieht es nicht so aus, als würden Sie das Feld $_POST['action'] irgendwo platzieren?

Wenn Sie sich auf die Methode des Sendens beziehen, die in dem öffnenden Tag <form> angegeben ist, dann ist das falsch.

Wenn Stenografie IF-Anweisung verwendet wird, um zu bestimmen, wie die Daten in die Datei something.php übergeben wird, dann könnten Sie tun

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

(Ich weiß, es scheint langatmig, aber die zusätzliche Überprüfung für $ _GET Daten kann weggelassen werden, es ist einfach da als Nachweis des Konzepts Typ Check).

Von diesem wird die Variable $data entweder mit den benutzerdefinierten Daten $ _GET oder $ _POST aus den Formularen auf den anderen Seiten festgelegt.

Ich hoffe, das hilft.