2015-02-07 120 views
-1

我尝试登录部分工作时出现问题。 我不断收到的问题是这样的:致命错误:无法在functions.php行中重新声明session_start()第25行

Fatal error: Cannot redeclare session_start() in public_html/login/functions.php on line 25

<?php 
require_once 'psl-config.php'; 

function session_start() { 
    $session_name = 'session_id'; // Set a custom session name 
    $secure = SECURE; 
    $httponly = true; 
    // Forces sessions to only use cookies. 
    if (ini_set('session.use_only_cookies', 1) === FALSE) { 
     header("Location: ../error.php?err=Could not initiate a safe session (ini_set)"); 
     exit(); 
    } 
    // Gets current cookies params. 
    $cookieParams = session_get_cookie_params(); 
    session_set_cookie_params($cookieParams["lifetime"], 
     $cookieParams["path"], 
     $cookieParams["domain"], 
     $secure, 
     $httponly); 
    // Sets the session name to the one set above. 
    session_name($session_name); 
    session_start();   // Start the PHP session 
    session_regenerate_id(true); // regenerated the session, delete the old one. // This stops JavaScript being able to access the session id. 

} 

行25只包含此:

}

还有更多的functions.php的,如果你想看到这一切:

<?php 
require_once 'psl-config.php'; 

function session_start() { 
    $session_name = 'session_id'; // Set a custom session name 
    $secure = SECURE; 
    $httponly = true; 
    // Forces sessions to only use cookies. 
    if (ini_set('session.use_only_cookies', 1) === FALSE) { 
     header("Location: ../error.php?err=Could not initiate a safe session (ini_set)"); 
     exit(); 
    } 
    // Gets current cookies params. 
    $cookieParams = session_get_cookie_params(); 
    session_set_cookie_params($cookieParams["lifetime"], 
     $cookieParams["path"], 
     $cookieParams["domain"], 
     $secure, 
     $httponly); 
    // Sets the session name to the one set above. 
    session_name($session_name); 
    session_start();   // Start the PHP session 
    session_regenerate_id(true); // regenerated the session, delete the old one. // This stops JavaScript being able to access the session id. 

} 
function login($email, $password, $mysqli) { 
    // Using prepared statements means that SQL injection is not possible. 
    if ($stmt = $mysqli->prepare("SELECT id, username, password, salt 
     FROM members 
     WHERE email = ? 
     LIMIT 1")) { 
     $stmt->bind_param('s', $email); // Bind "$email" to parameter. 
     $stmt->execute(); // Execute the prepared query. 
     $stmt->store_result(); 

     // get variables from result. 
     $stmt->bind_result($user_id, $username, $db_password, $salt); 
     $stmt->fetch(); 

     // hash the password with the unique salt. 
     $password = hash('sha512', $password . $salt); 
     if ($stmt->num_rows == 1) { 
      // If the user exists we check if the account is locked 
      // from too many login attempts 

      if (checkbrute($user_id, $mysqli) == true) { 
       // Account is locked 
       // Send an email to user saying their account is locked 
       return false; 
      } else { 
       // Check if the password in the database matches 
       // the password the user submitted. 
       if ($db_password == $password) { 
        // Password is correct! 
        // Get the user-agent string of the user. 
        $user_browser = $_SERVER['HTTP_USER_AGENT']; 
        // XSS protection as we might print this value 
        $user_id = preg_replace("/[^0-9]+/", "", $user_id); 
        $_SESSION['user_id'] = $user_id; 
        // XSS protection as we might print this value 
        $username = preg_replace("/[^a-zA-Z0-9_\-]+/", 
                   "", 
                   $username); 
        $_SESSION['username'] = $username; 
        $_SESSION['login_string'] = hash('sha512', 
           $password . $user_browser); 
        // Login successful. 
        return true; 
       } else { 
        // Password is not correct 
        // We record this attempt in the database 
        $now = time(); 
        $mysqli->query("INSERT INTO login_attempts(user_id, time) 
            VALUES ('$user_id', '$now')"); 
        return false; 
       } 
      } 
     } else { 
      // No user exists. 
      return false; 
     } 
    } 
} 
function checkbrute($user_id, $mysqli) { 
    // Get timestamp of current time 
    $now = time(); 

    // All login attempts are counted from the past 2 hours. 
    $valid_attempts = $now - (2 * 60 * 60); 

    if ($stmt = $mysqli->prepare("SELECT time 
          FROM login_attempts 
          WHERE user_id = ? 
          AND time > '$valid_attempts'")) { 
     $stmt->bind_param('i', $user_id); 

     // Execute the prepared query. 
     $stmt->execute(); 
     $stmt->store_result(); 

     // If there have been more than 5 failed logins 
     if ($stmt->num_rows > 5) { 
      return true; 
     } else { 
      return false; 
     } 
    } 
} 
function login_check($mysqli) { 
    // Check if all session variables are set 
    if (isset($_SESSION['user_id'], 
         $_SESSION['username'], 
         $_SESSION['login_string'])) { 

     $user_id = $_SESSION['user_id']; 
     $login_string = $_SESSION['login_string']; 
     $username = $_SESSION['username']; 

     // Get the user-agent string of the user. 
     $user_browser = $_SERVER['HTTP_USER_AGENT']; 

     if ($stmt = $mysqli->prepare("SELECT password 
             FROM members 
             WHERE id = ? LIMIT 1")) { 
      // Bind "$user_id" to parameter. 
      $stmt->bind_param('i', $user_id); 
      $stmt->execute(); // Execute the prepared query. 
      $stmt->store_result(); 

      if ($stmt->num_rows == 1) { 
       // If the user exists get variables from result. 
       $stmt->bind_result($password); 
       $stmt->fetch(); 
       $login_check = hash('sha512', $password . $user_browser); 

       if ($login_check == $login_string) { 
        // Logged In!!!! 
        return true; 
       } else { 
        // Not logged in 
        return false; 
       } 
      } else { 
       // Not logged in 
       return false; 
      } 
     } else { 
      // Not logged in 
      return false; 
     } 
    } else { 
     // Not logged in 
     return false; 
    } 
} 
function esc_url($url) { 

    if ('' == $url) { 
     return $url; 
    } 

    $url = preg_replace('|[^a-z0-9-~+_.?#=!&;,/:%@$\|*\'()\\x80-\\xff]|i', '', $url); 

    $strip = array('%0d', '%0a', '%0D', '%0A'); 
    $url = (string) $url; 

    $count = 1; 
    while ($count) { 
     $url = str_replace($strip, '', $url, $count); 
    } 

    $url = str_replace(';//', '://', $url); 

    $url = htmlentities($url); 

    $url = str_replace('&amp;', '&#038;', $url); 
    $url = str_replace("'", '&#039;', $url); 

    if ($url[0] !== '/') { 
     // We're only interested in relative links from $_SERVER['PHP_SELF'] 
     return ''; 
    } else { 
     return $url; 
    } 
} 

从问题出发,当我点击登录按钮或注册它没有做任何事情的问题开始。 那里被宣布和会话开始为

sec_session_start();

,但它给了我所有类型的警告和一个致命的错误,我固定通过删除“秒”部分,只留下“在session_start();”。 早些时候警告和错误disapeared并重新声明时出现的错误,并试图研究的东西,并试图修复不同,但他们都最终成为语法错误等

我用于登录系统教程是: How to Create a Secure Login Script in PHP and MySQL

+1

PHP已经与名称的内置的功能:'在session_start()'你必须选择别名 – Rizier123 2015-02-07 15:02:03

+0

供参考:如果你还没有看到它,你可以在这里参观:http://stackoverflow.com/tour,看看这个网站是如何工作的! (欢迎使用StackOverflow:D) – Rizier123 2015-02-07 15:11:43

回答

3

session_start()是一个内置的PHP函数。你不能为命名你自己的同名功能。

一个简单的解决方法是调用您的函数start_session()或避免命名混淆,session_manage()

+0

也许命名与内置函数非常相似的函数并不是那么好,否则你不能分辨它们和(更大的)错字之间的区别,并且你写'start_session()而不是'session_start()'。但是这更多的是从个人的角度来看。 – Rizier123 2015-02-07 15:06:27

+0

@ Rizier123我不反对。 – 2015-02-07 15:06:58

3

session_start是一个built-in函数名称在PHP中。

使用不同的名称。例如:my_session_start

在较大的项目,可以动态检查,如果一个函数已经定义:

// escape characters 
if (!function_exists('e')) { 
    function e(str){ 
     return htmlspecialchars(str); 
    } 
} 
相关问题