sql >> Database >  >> RDS >> Mysql

php mysql + sessieproblemen

In index.php je moet deze if-voorwaarde bovenaan plaatsen na 'session_start();'

if($_SESSION['username'])
{
    header("Location: home.php");
    exit();
}

In while-lus moet het header("Location: home.php"); . zijn in plaats van header("Location: index.php");

In home.php pagina die u bovenaan moet plaatsen na het openen van de php-tag

ob_start();
session_start();

Ik hoop dat het zal werken.

++++++++++++++++++++++++++++++++++++++++++

Gebruik deze codeindex.php

<?php
require_once('connect.php');
ob_start();
session_start();
//checked wether the user is loged in  or not 

$user = $_SESSION['username'];

if($_SESSION['username'])
{
    $user = $_SESSION['username'];
    header("Location: home.php");
    exit();
}

// login script
if(isset($_POST['username'])&& isset($_POST['password']))
{
    $user_login = preg_replace('#[^A-Za-z0-9]#i', '', $_POST['username']);
    $user_password = preg_replace('#[^A-Za-z0-9]#i', '', $_POST['password']);
    $md5password = md5($user_password);
    $sql = mysql_query("SELECT id FROM members WHERE username = '".$user_login."' AND password = '".$user_password."'") or die ("could not select from database");

    $userCount = mysql_num_rows($sql);
    if($userCount ==1)
    {
        while($row = mysql_fetch_array($sql))
        {

            $id = $row['id'];
        }

        $_SESSION['id'] = $id;
        $_SESSION['username'] = $user_login;
        $_SESSION['password'] = $user_password;
        header("Location: home.php");
        exit();
    }
    else
    {
         echo "that info is incorrect";
         exit();
    }
}


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form action="login.php" method="post">

<input name="username" type="text" value="username" size="32" />
<input name="pass" type="password" value="password" size="32" />
<input name="login" type="submit" value="login" />

</form>

</body>
</html>
<?php  ob_end_flush(); ?>

home.php

<?php
ob_start();
session_start();

//home.php
$user = $_SESSION['username'];
if(!isset($_SESSION['username']))
{
    header("Location: index.php");
    exit();
}
else
{

    echo "hi $user you are loged in //Welcome to our website <a href=\"logout.php\">Logout</a>";


}
?>

logout.php klopt



  1. error:'Kan geen verbinding maken met lokale MySQL-server via socket '/var/run/mysqld/mysqld.sock' (2)' -- Ontbrekend /var/run/mysqld/mysqld.sock

  2. kan geen gegevens van de ene tabel naar een andere tabel opslaan

  3. AM/PM werkt niet met tussen in sql

  4. Rails Postgres functionele indexen