Another php session problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kong Chun Ho
    New Member
    • Jul 2010
    • 34

    #1

    Another php session problem

    This is my code"
    Code:
    <?php if(!session_start()) { exit(); }else{ session_start(); $sessid = session_id(); }?>
    // some html code
          <?
    	  // here is part a
    	  $_SESSION['x'] = "hello world!"; echo $_SESSION['x'];
    	  if (isset($_GET['admin'])) {
    	  include("include/admin.inc.php");
    	  }else if ($_GET['a'] == "l") {
    	  $query = "SELECT * FROM `login_attempt` WHERE ip = '$userip';";
    	  $query = mysql_query($query);
    	  if(mysql_num_rows($query) == 0) {
    	  $query1 = 'INSERT INTO `login_attempt` (`ip` ,`attempt_left` ,`expire`) VALUES ("' . $userip . '", "5", DATE_ADD(NOW(), INTERVAL 1 HOUR));';
    	  mysql_query($query1);
    	  }
    	  if(mysql_num_rows($query) != 0) {
    	   while($info = mysql_fetch_array($query)) {
    	   $log_left_times = $info['attempt_left'];
    	   $log_exp = $info['expire'];
    	   }
    	  }
    	  require('func/CalculateMonthSecond.func.php');
    	  $log_exp = str_split($log_exp);
    	  $year = $log_exp[0] . $log_exp[1] . $log_exp[2] . $log_exp[3];
    	  $month = $log_exp[5] . $log_exp[6];
    	  $day = $log_exp[8] . $log_exp[9];
    	  $hour = $log_exp[11] . $log_exp[12];
    	  $minute = $log_exp[14] . $log_exp[15];
    	  $second = $log_exp[17] . $log_exp[18];
    	  $y_s = 31536000*$year;
    	  $m_s = CalculateMonthSecond($year, $month);
    	  $d_s = $day*86400;
    	  $h_s = $hour*3600;
    	  $m_s2 = $minute*60;
    	  $s_s = $second;
    	  $expire_second = $y_s+$m_s+$d_s+$h_s+$m_s2+$s_s;
    	  $year2 = date('Y');
    	  $month2 = date('m');
    	  $day2 = date('d');
    	  $hour2 = date('H');
    	  $minute2 = date('i');
    	  $second2 = date('s');
    	  $y_s2 = 31536000*$year2;
    	  $m_s2 = CalculateMonthSecond($year2, $month2);
    	  $d_s2 = $day2*86400;
    	  $h_s2 = $hour2*3600;
    	  $m_s3 = $minute2*60;
    	  $s_s2 = $second2;
    	  $now_second = $y_s2+$m_s2+$d_s2+$h_s2+$m_s3+$s_s2;
    	  if($expire_second < $now_second) {
    	  $query_updEXP = 'UPDATE `login_attempt` SET `attempt_left` = "5"' . ' WHERE `ip` = "' . $userip . '";';
    	  mysql_query($query_updEXP);
    	  }
    	  $query = "SELECT * FROM `login_attempt` WHERE ip = '$userip';";
    	  $query = mysql_query($query);
    	  if(isset($_POST['account']) && isset($_POST['password'])) {
    	  if ($expire_second < $now_second) {
    	  $expired = 1;
    	  }else{
    	  $expired = 0;
    	  }
    	  $account = SQLInjectionFilter($_POST['account']);
    	  $password = hash("sha256", $_POST['password']);
          $query = "Select * FROM `user` WHERE username = $account AND password = '$password';";
    	  $query = mysql_query($query);
    	  if($log_left_times <= 0 && $expired == 0) {
    	  $query_z = 'update `login_attempt` set `expire` = DATE_ADD(NOW(), INTERVAL 1 HOUR) WHERE `ip` = "' . $userip . '";';
    	  mysql_query($query_z);
    	  $s = 3;
    	  }else if ($log_left_times >= 0 && expired == 1) {
    	    if(mysql_num_rows($query) != 0) {
       	    $_SESSION['logined'] == '1';
    	    $_SESSION['logined_time'] == date("r");
            $_SESSION['account'] == $_POST['account'];
            echo $_SESSION['logined'];
    	    // here is part b
    	    $_SESSION['x'] = "hello world!"; echo $_SESSION['x'];
    		$s = 2;
    		}else{
    		$log_left_times = $log_left_times-1;
    		$query_updEXP_2 = 'UPDATE `login_attempt` SET `attempt_left` = "' . $log_left_times . '"' . ' WHERE `ip` = "' . $userip . '";';
    		$query_updEXP_3 = 'update `login_attempt` set `expire` = DATE_ADD(NOW(), INTERVAL 1 HOUR) WHERE `ip` = "' . $userip . '";';
    		mysql_query($query_updEXP_2);
    		mysql_query($query_updEXP_3);
    		$s = 1;
    		}
    	  }else if ($log_left_times > 0) {
    	    if(mysql_num_rows($query) != 0) {
       	    $_SESSION['logined'] == '1';
    	    $_SESSION['logined_time'] == date("r");
            $_SESSION['account'] == $_POST['account'];
            echo $_SESSION['logined'];
    	    // here is part b
    	    $_SESSION['x'] = "hello world!"; echo $_SESSION['x'];
    		$s = 2;
    		}else{
    		$log_left_times = $log_left_times-1;
    		$query_updEXP_2 = 'UPDATE `login_attempt` SET `attempt_left` = "' . $log_left_times . '"' . ' WHERE `ip` = "' . $userip . '";';
    		$query_updEXP_3 = 'update `login_attempt` set `expire` = DATE_ADD(NOW(), INTERVAL 1 HOUR) WHERE `ip` = "' . $userip . '";';
    		mysql_query($query_updEXP_2);
    		mysql_query($query_updEXP_3);
    		$s = 1;
    		}
    	  }
    	}
    	  require("include/login.inc.php");
    	  }else if ($_GET['a'] == "r") {
    	  include("include/register.inc.php");
      	  }else if ($_GET['a'] == "c") {
      	  include("include/contact.inc.php");
      	  }else if ($_GET['a'] == "f") {
     	  include("include/faq.inc.php");
    	  }else if ($_GET['a'] == "t") {
    	  include("include/tos.inc.php");
    	  }else if ($_GET['a'] == "a") {
    	  require ("include/account.inc.php");
       	  }else if ($_GET['a'] == "b") {
    	  include ("include/banner.inc.php");
    	  }else	if (!isset($_GET['a']) OR $_GET['a'] == "" OR $_GET['a'] == "h") {  
          include("include/index.inc.php");
    	  }
          ?>
    // some html code
    in part a, it can print out hello world!, but in part b, it didn't print out anything!

    So how to fix this?

    Thanks!
  • dlite922
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

    #2
    For basic stuff like this, you sometimes have to reduce your code to absolute minimum and hard code some data if needed to try to pin point the problem.

    This is a useful skill that all programmers should have, thus you should not be robbed of opportunities to do this yourself.

    1. Save this file and rename it as something else

    2. Take out all the queries and logic

    3. Leave the session code the way it "should" work and debug from there.


    Good luck,


    Dan

    Comment

    • matthewlkc
      New Member
      • Jul 2010
      • 1

      #3
      my father say
      may be your "else" is not match with"if"
      check it angin

      Comment

      • Kong Chun Ho
        New Member
        • Jul 2010
        • 34

        #4
        Thanks, my problem sloved, also, i want choose both of you as the best ans. but i found it there is some if and else problem in my script, however thanks dan!

        Comment

        Working...