2017-03-14 4 views
1

現在、自分でログインコードをしようとしています。誰かが私のコードを調べて、誤りがあったと言えば、私はずっと義務づけられているでしょう。私のPHPコードがなぜ動作するのか理解するのに助けが必要

初めての組織の不在のため申し訳ありません。

私の母国語(母国語ではない)のために、私のポルトガル語は申し訳ありません。

-index.php 
    <html> 
    <head> 
    <?php session_start(); ?> 
    <title> Login Page </title> 

    </head> 

    <body> 

    <form action="login2.php" method="post"> 

    <table width="200" border="0"> 
    <tr> 
    <td> UserName</td> 
    <td> <input type="text" name="user" > </td> 
    </tr> 
    <tr> 
    <td> PassWord </td> 
    <td><input type="password" name="pass"></td> 
    </tr> 
    <tr> 
    <tr> 
    <td> Email </td> 
    <td><input type="email" name="email"></td> 
    </tr> 
    <tr> 
    <td> <input type="submit" name="login" value="LOGIN"></td> 
    <td><a href="logout.php">Logout</a></td> 
    </tr> 
    </table> 
    </form> 

    </body> 
    </html> 

    Home.php 
    <?php 
    require_once 'database.php'; 
    $res=mysql_query("SELECT * FROM users WHERE id=".$_SESSION['user']); 
    $userRow=mysql_fetch_array($res); ?> 
    <html> 
    <head> 
    <title> Home </title> 
    </head> 
    <body> 
    <?php 
    if(!isset($_SESSION['use'])) 
    { 
    header("Location:Login.php"); 
    } 
    echo $userRow['userEmail']; 
    echo "Login Success"; 
    echo "<a href='logout.php'> Logout</a> "; 
    ?> 
    </body> 
    </html> 

    logout.php 
    <?php 
    session_start(); 
    echo "Logout Successfully "; 
    session_destroy(); // function that Destroys Session 
    header("Location: Login.php"); 
    ?> 

    database.php 
    <?php 
    // this will avoid mysql_connect() deprecation error. 
    error_reporting(~E_DEPRECATED & ~E_NOTICE); 
    // but I strongly suggest you to use PDO or MySQLi. 

    define('DBHOST', 'localhost'); 
    define('DBUSER', 'root'); 
    define('DBPASS', ''); 
    define('DBNAME', 'database_sof'); 

    $conn = mysql_connect(DBHOST,DBUSER,DBPASS); 
    $dbcon = mysql_select_db(DBNAME); 
    ?> 



    login2.php 
    <?php 
    require_once 'database.php'; 
    if(isset($_SESSION['user'])) { 
    header("Location:home.php"); 
    } 
    if(isset($_POST['login'])) { 
    $user = $_POST['user']; 
    $pass = $_POST['pass']; 
    $email = $_POST['email']; 

    if(empty($user)){ 
    echo "Please enter your username.";} 
    if(empty($pass)){ 
    echo "Please enter your passoword.";} 
    if(empty($email)){ 
    echo "Please enter your email.";} 
    $res=mysql_query("SELECT id, username, password FROM users WHERE email='$email'"); 
    $row=mysql_fetch_array($res); 
    $count = mysql_num_rows($res); 
    if($count == 1 && $row['password']==$pass) { 
    $_SESSION['user'] = $row['id']; 
    session_start(); 
    header("Location: home.php");} else { 
    echo $user; 
    echo "<br>"; 
    echo $pass; 
    echo "<br>"; 
    echo $email; 
    echo "<br>"; 
    echo $count; 
    echo "<br>"; 
    echo $row['password']; 
    echo "<br>"; 
    echo "Incorrect Credentials, Try again..."; 
    }}?> 
+2

あなたがPHPのバージョンを使用していますか? * mysql_ *関数は廃止され、mysqliで置き換えられました。 – Sablefoste

+3

HTML出力の前にsession_start()を実行する必要があります。 –

+0

@KoalaYeung助けてくれてありがとう。 –

答えて

1

(申し訳ありません私の英語のため)

あなたのファイルに$ _SESSION変数を使用したい場合は、あなたが(任意の出力前にそのファイルとの冒頭にsession_start()を記述する必要がありとしてコアラYeungは言った)。

somefile.php:

<?php 
session_start(); 
... 
//now you can read or edit $_SESSION 

$_SESSION['bar'] = "bar"; 

$foo = $_SESSION['foo']; 
関連する問題