2013-08-03 12 views
-4

我是一個新手程序員。我剛開始學習php。此時我嘗試登錄頁面。對於登錄,我有以下的代碼名爲「的index.php」:解析錯誤:語法錯誤,意外'索引'

<!-- LOGIN FORM in: admin/index.php --> 
<form method="post" action="restricted.php"> 
    <p><label for="u_name">username:</label></p> 
    <p><input type="text" name="u_name" value=""></p> 

    <p><label for="u_pass">password:</label></p> 
    <p><input type="password" name="u_pass" value=""></p> 

    <p><button type="submit" name="go">log me in</button></p> 
</form> 
<!-- A paragraph to display eventual errors --> 
<p><strong><?php if(isset($error)){echo $error;} ?></strong></p> 

<?php #admin/index.php 
      #####[make sure you put this code before any html output]##### 

//connect to server 
$dbc = mysqli_connect('localhost','root','') or 
      die('could not connect: '. mysqli_connect_error()); 

//select db 
mysqli_select_db($dbc, 'simple_login') or die('no db connection'); 

//check if the login form has been submitted 
if(isset($_POST['go'])){ 
    #####form submitted, check data...##### 

     //step 1a: sanitise and store data into vars (storing encrypted password) 
    $usr = mysqli_real_escape_string($dbc, htmlentities($_POST['u_name'])); 
    $psw = SHA1($_POST['u_pass']) ; //using SHA1() to encrypt passwords 

     //step2: create query to check if username and password match 
    $q = "SELECT * FROM users WHERE name='$usr' AND pass='$psw' "; 

    //step3: run the query and store result 
    $res = mysqli_query($dbc, $q); 
// 
    //make sure we have a positive result 
    if($res!=false){ 
     if(mysqli_num_rows($res)== 1){ 
      ######### LOGGING IN ########## 
      //starting a session 
      session_start(); 

      //creating a log SESSION VARIABLE that will persist through pages 
      $_SESSION['log'] = 'in'; 

      //redirecting to restricted page 
      header('location:restricted.php'); 
     } else 
     { 
      //create an error message 
      $error = 'Wrong details. Please try again'; 
     } 
    } 
}//end isset go 
?> 
<!-- HTML FORM GOES HERE --> 

然後我就在一個名爲「restricted.php」 PHP頁面寫了下面的代碼。該頁面的代碼如下:

<?php #admin/restricted.php 
      #####[make sure you put this code before any html output]##### 

//starting the session 
session_start(); 

//checking if a log SESSION VARIABLE has been set 
if(!isset($_SESSION['log']) || ($_SESSION['log'] != 'in')){ 
     //if the user is not allowed, display a message and a link to go back to login page 
    echo "You are not allowed. <a href="index.php">back to login page</a>"; 

     //then abort the script 
    exit(); 
} 

/** 
*  #### CODE FOR LOG OUT #### click here to see the logout tutorial 
*/ 

?> 
<!-- RESTRICTED PAGE HTML GOES HERE --> 

但是,讓jusername和密碼文件瀏覽器後,顯示我下面的錯誤:

Parse error: syntax error, unexpected 'index' (T_STRING), expecting ',' or ';' in C:\xampp\htdocs\restricted.php on line 10 

爲什麼發生這個錯誤?我該如何解決這個問題? PLZ幫我

+1

'echo「你是不允許的back to login page」;'改爲'echo'你是不​​允許的。 back to login page';'。 –

+0

^單引號而不是雙引號,因爲您的字符串中已經有雙引號。 –

+0

是的。我知道了 。謝謝戴夫。 –

回答

3

看語法高亮,你需要逃避你的HTML的報價是這樣的:

echo "You are not allowed. <a href=\"index.php\">back to login page</a>"; 

或者用周圍像這樣整件事單引號代替雙引號:

echo 'You are not allowed. <a href="index.php">back to login page</a>'; 
相關問題