简体   繁体   English

需要帮助弄清楚为什么我的php代码惠特会话inst工作

[英]Need help figuring out why my php code whit sessions inst working

Im currently trying to do a login code by myself and i cant figure out how ot work whit session. 我目前正试图自己做一个登录代码,我无法弄清楚会话如何工作。 If anyone can examine my code and say were are the errors i would be much obliged. 如果有人可以检查我的代码并说是错误,那我将非常有责任。

Sorry for the lack of organization first time posting here. 抱歉,由于缺乏组织,第一次在此发布。

Sorry for my english(not my native language), im portuguese. 对不起,我的英语(不是我的母语)是葡萄牙语。

-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...";
    }}?>

(sorry for my english) (对不起我的英语不好)

if you want to use $_SESSION variable in your file, you must write session_start() at the beginning of that file AND before any output (as Koala Yeung said). 如果要在文件中使用$ _SESSION变量,则必须在该文件的开头以及任何输出之前编写session_start()(如Koala Yeung所说)。

somefile.php: somefile.php:

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

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

$foo = $_SESSION['foo'];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我需要帮助弄清楚为什么Google reCAPTCHA无法在我网站的.php联系表上工作 - I need help figuring out why Google reCAPTCHA isn't working on my site's .php contact form 需要帮助弄清楚为什么 Fullcalendar 没有显示我的事件。 - 没有错误显示 - Need help figuring out why Fullcalendar isn't showing my events. - no errors showing 我需要帮助找出使用文件和statemtents这个php问题 - i need help figuring out this php problem using files and if statemtents 制作路由器,需要帮助弄清楚为什么我得到403 - making a router, need help figuring out why I get 403 MySQL慢查询日志向我的MySQL语句添加了一些不是我的代码产生的东西,需要帮助弄清楚这一切的含义 - MySQL slow query log has some stuff added to my MySQL statements that isn't coming from my code, need help figuring out what it all means 需要帮助找出哪些PDO数据库驱动程序包含一个information_schema数据库(PHP) - Need help figuring out which PDO database drivers contain an information_schema db (PHP) 我需要PHP会话的帮助 - I need help with PHP sessions 找出htaccess规则以及它们无法正常工作的原因有点帮助 - a little help in figuring out htaccess rules and why they aren't working correctly 需要一些帮助来确定如何进行此验证 - need some help figuring out how to approach this validation 需要帮助找出MySQL查询以计数是否一定数量 - Need help figuring out MySQL query to count if a certain number
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM