2016-07-10 22 views
1

ログインしていて、コードが機能していないかどうかを確認しようとしています。ここに私のlogin.phpここログインを確認すると、常にホームページに送信されます(ユーザがログインしていない操作)

<?php 
    $servername = "localhost"; 
    $username = ""; 
    $passcode = ""; 
    $dbname = ""; 

    $conn = mysqli_connect($servername, $username, $passcode, $dbname); 

    if (!$conn) 
    { 
     echo ' 
      <script type="text/javascript"> 
       Notification.requestPermission().then(function(result) { 
        console.log(result); 
       }); 
       var notification = new Notification("Smart Lead Advertisement Error", { 
        icon: "app/logo.png", 
        body: "Failed to establish a connection to the database" 
       }); 
      </script> 
     '; 
     die(); 
    } 
    else 
    { 
     if (isset($_POST['username'])&&isset($_POST['pass'])) 
     { 
      $username = $_POST['username']; 
      $pass = $_POST['pass']; 
      if (empty($username)) 
      { 
       echo '    
        <script type="text/javascript"> 
         Notification.requestPermission().then(function(result) { 
          console.log(result); 
         }); 
         var notification = new Notification("Smart Lead Advertisement Error", { 
          icon: "app/logo.png", 
          body: "Please provide a username" 
         }); 
        </script> 
       '; 
      } 
      if (empty($pass)) 
      { 
       echo '  
        <script type="text/javascript"> 
         Notification.requestPermission().then(function(result) { 
          console.log(result); 
         }); 
         var notification = new Notification("Smart Lead Advertisement Error", { 
          icon: "app/logo.png", 
          body: "Please provide a password" 
         }); 
        </script> 
       '; 
      } 
      $query = "SELECT `ID` FROM `loginsystem` WHERE `username` = '$username' AND `passcode` = '$pass'"; 
      if ($querySucceded = mysqli_query($conn, $query)) 
      { 
       $queryValidate = mysqli_num_rows($querySucceded); 
       if ($queryValidate == 0) 
       { 
        echo ' 
         <script type="text/javascript"> 
          Notification.requestPermission().then(function(result) { 
           console.log(result); 
          }); 
          var notification = new Notification("Smart Lead Advertisement Error", { 
           icon: "app/logo.png", 
           body: "Incorrect login credentials" 
          }); 
         </script> 
        '; 
       } 
       else if ($queryValidate == 1) 
       { 
        $userId = mysqli_fetch_assoc($querySucceded); 
        $_SESSION['ID'] = $userId; 
        echo ' 
        <script type="text/javascript"> 
         window.location.href="app/php/checkLogin.php"; 
         Notification.requestPermission().then(function(result) { 
          console.log(result); 
         }); 
         var notification = new Notification("Smart Lead Advertisement", { 
          icon: "app/logo.png", 
          body: "Login success" 
         }); 
        </script>'; 
       } 
      } 
      else 
      { 
       echo ' 
        <script type="text/javascript"> 
         Notification.requestPermission().then(function(result) { 
          console.log(result); 
         }); 
         var notification = new Notification("Smart Lead Advertisement Error", { 
          icon: "app/logo.png", 
          body: "Query could not be performed" 
         }); 
        </script> 
       '; 
      } 
     } 
    } 
?> 

私checkLogin.php

<?php 
    require("../../php/login.php"); 
    if (isset($_SESSION['username']) && !empty($_SESSION['username'])) 
    { 
     $_SESSION['username'] = $user; 
     echo " 
      <script type='text/javascript'> 
       console.log('Logged user: $user'); 
      </script> 
     "; 
    } 
    else 
    { 
     echo '<script type="text/javascript">window.location.href = "http://smartlead.online/"</script>'; 
    } 
?> 

である。しかしかかわらず、ログイン状態のそれは常にホームページに私をリードしています。私は、ユーザーがログインしている場合は、ページに滞在することができるようにしたいことをしたいいけないと、彼らはコメント

<?php 
    require("../../php/login.php"); 
    if (isset($_SESSION['ID']) && !empty($_SESSION['ID'])) 
    { 
     $_SESSION['ID'] = $user; 
     echo " 
      <script type='text/javascript'> 
       console.log('Logged user: $user'); 
      </script> 
     "; 
    } 
    else 
    { 
     echo '<script type="text/javascript">window.location.href = "http://smartlead.online/"</script>'; 
    } 
?> 

ありません良いに基づいて

修正されていない場合、ログイン行くことに。まだ動作しません。

第二編集

<?php 
    require("../../php/login.php"); 
    session_start(); 
    if (isset($_SESSION['ID']) && !empty($_SESSION['ID'])) 
    { 
     $_SESSION['ID'] = $user; 
     echo " 
      <script type='text/javascript'> 
       console.log('Logged user: $user'); 
      </script> 
     "; 
    } 
    else 
    { 
     echo '<script type="text/javascript">window.location.href = "http://smartlead.online/"</script>'; 
    } 
?> 

使用済みのsession_start();セッションが開始されることはありませんので、まだdoesntの仕事このセットアップPHPで

編集3

<?php 
    $servername = "localhost"; 
    $username = "smartlea_reborn"; 
    $passcode = "1201920203Ir"; 
    $dbname = "smartlea_general_database"; 

    $conn = mysqli_connect($servername, $username, $passcode, $dbname); 

    if (!$conn) 
    { 
     echo ' 
      <script type="text/javascript"> 
       Notification.requestPermission().then(function(result) { 
        console.log(result); 
       }); 
       var notification = new Notification("Smart Lead Advertisement Error", { 
        icon: "app/logo.png", 
        body: "Failed to establish a connection to the database" 
       }); 
      </script> 
     '; 
     die(); 
    } 
    else 
    { 
     if (isset($_POST['username'])&&isset($_POST['pass'])) 
     { 
      $username = $_POST['username']; 
      $pass = $_POST['pass']; 
      if (empty($username)) 
      { 
       echo '    
        <script type="text/javascript"> 
         Notification.requestPermission().then(function(result) { 
          console.log(result); 
         }); 
         var notification = new Notification("Smart Lead Advertisement Error", { 
          icon: "app/logo.png", 
          body: "Please provide a username" 
         }); 
        </script> 
       '; 
      } 
      if (empty($pass)) 
      { 
       echo '  
        <script type="text/javascript"> 
         Notification.requestPermission().then(function(result) { 
          console.log(result); 
         }); 
         var notification = new Notification("Smart Lead Advertisement Error", { 
          icon: "app/logo.png", 
          body: "Please provide a password" 
         }); 
        </script> 
       '; 
      } 
      $query = "SELECT `ID` FROM `loginsystem` WHERE `username` = '$username' AND `passcode` = '$pass'"; 
      if ($querySucceded = mysqli_query($conn, $query)) 
      { 
       $queryValidate = mysqli_num_rows($querySucceded); 
       if ($queryValidate == 0) 
       { 
        echo ' 
         <script type="text/javascript"> 
          Notification.requestPermission().then(function(result) { 
           console.log(result); 
          }); 
          var notification = new Notification("Smart Lead Advertisement Error", { 
           icon: "app/logo.png", 
           body: "Incorrect login credentials" 
          }); 
         </script> 
        '; 
       } 
       else if ($queryValidate == 1) 
       { 
        session_start(); 
        $userId = mysqli_fetch_assoc($querySucceded); 
        $_SESSION['ID'] = $userId; 
        echo ' 
        <script type="text/javascript"> 
         window.location.href = "http://smartlead.online/app/index.php"; 
         Notification.requestPermission().then(function(result) { 
          console.log(result); 
         }); 
         var notification = new Notification("Smart Lead Advertisement", { 
          icon: "app/logo.png", 
          body: "Login success" 
         }); 
        </script>'; 
       } 
      } 
      else 
      { 
       echo ' 
        <script type="text/javascript"> 
         Notification.requestPermission().then(function(result) { 
          console.log(result); 
         }); 
         var notification = new Notification("Smart Lead Advertisement Error", { 
          icon: "app/logo.png", 
          body: "Query could not be performed" 
         }); 
        </script> 
       '; 
      } 
     } 
    } 
?> 

checkLogin.php

<?php 
    session_start(); 
    require("../../php/login.php"); 
    echo $_SESSION['ID']; 
    if (isset($_SESSION['ID']) && !empty($_SESSION['ID'])) 
    { 
     $_SESSION['ID'] = $user; 
     echo " 
      <script type='text/javascript'> 
       console.log('Logged user: $user'); 
      </script> 
     "; 
    } 
    else 
    { 
     echo 'Error'; 
    } 
?> 
+0

['header(" Location:yoursite.com ")'](http://php.net/header)を使用してリダイレクトする必要があります。また、ユーザがリダイレクトを無効にした場合に 'header'、' exit() 'を使用した後で覚えておいてください。 – Terminus

+0

私は実際にajaxを使用しています。ヘッダーはajax呼び出しで機能しません。 – user019911019019101901

+0

私はその周りに仕事を見つけなければなりませんでした。 – user019911019019101901

答えて

0

は、セッション変数を使用することができません。 checkLogin.phpファイルの上にsession_start();を追加する必要があります。

関連する問題