I don't get it. I try the code and it works when i log out it doesn't work when i log in again.

test.php

<?php
    session_start();
    
	var_dump ($_SESSION['username']);
    var_dump ($_SESSION['loggedin']);
	
    function is_logged()
    {
    if(isset($_SESSION['loggedin']))
    {
    return true;
    }else{
    return false;
    }
    }
     
    if(is_logged())
    {
    echo "Welcome, ". $_SESSION['username'] ."!";
    }else{
    echo "Welcome, Guest!";
    }

validapass.php

<?php
    session_start();
     
    include "db_connect.php";
    mysql_select_db($dbname, $connect);
     
    $username = $_POST['username'];
    $pass = $_POST['pass'];
     
    $sql="SELECT username, password FROM alunos WHERE username='". $username. "' AND '". $pass ."'";
    $resultado = mysql_query($sql, $connect) or die(mysql_error());
    if(mysql_affected_rows() == 1)
    {
	$_SESSION['username'] = $username;
    $_SESSION['loggedin'] = 1;
    
	header("Location: test.php");
    }else{
    header("Location: login_form.php");
    }
    mysql_close($con);
     
    ?>

It's really weird haha! Can you please post what you are using for "logout"?

<?php
    session_start();
    Session_destroy();
    header("Location: index.php");
?>

Just to clarify, it works, then when you sign out.. It redirects you the page that says "Welcome, Guest!" and then when you try and sign back in, does it say "Welcome [blank]" or does it redirect you back to the login form?

This is really, really weird.. It's working perfectly fine on my system :(

Yes, it worked. after i sign out i tried to sign in again and from now it goes to login_form.php

That sounds to me like it's not sessions, it's something to do with your database.. In "validpass.php" you check to see if the username/password is correct or not, if it's not correct then it will redirect you to the login form..

Are you sure your SQL query is right AND you're entering the correct details? :)

Change

$sql="SELECT username, password FROM alunos WHERE username='". $username. "' AND '". $pass ."'";

To:

$sql = "SELECT username, password FROM alunos WHERE username='$username' AND password='$pass'";

:)

I can't send you the .zip, i'm trying to see what's wrong. If i get something i'll tell ;)

<?php
session_start();

include "db_connect.php";

$username = $_POST['username'];
$pass = $_POST['pass'];

mysql_select_db($dbname, $connect);
$sql="SELECT username, password FROM alunos WHERE username='". $username. "'";

$resultado = mysql_query($sql, $connect) or die(mysql_error());

$num = mysql_fetch_assoc($resultado);

if ($username == $num['username'] AND $pass == $num['password']) {
        echo "Yes logged in :)";
        } else {
        echo "Could not log in :(";
        }
mysql_close($con);
?>

Please can you run this code, login and tell me if it says:

Yes logged in :)
OR
Could not log in :(

Says: "Yes logged in :)"

<?php
session_start();

include "db_connect.php";

$username = $_POST['username'];
$pass = $_POST['pass'];

mysql_select_db($dbname, $connect);
$sql="SELECT username, password FROM alunos WHERE username='". $username. "'";

$resultado = mysql_query($sql, $connect) or die(mysql_error());

$num = mysql_fetch_assoc($resultado);

if ($username == $num['username'] AND $pass == $num['password']) {
        $_SESSION['username'] = $username;
        header("Location: test.php"); // sorry xD
        } else {
        echo "Could not log in :(";
        }
mysql_close($con);
?>

Try that now =) I think there was a problem with the previous SQL statement!

It worked. :D

Now let's pray that log out and log back in to work.

41 replies later and ripping my hair out ;) woo!

Let me know how you get on!

F**K YEAH it works, logout login logout login :)

Thanks a lot man, thanks for being patient too.

haha woo :)! You're welcome, pleasure! If you ever need anything else, feel free to ask! I hope this project goes well for you. Take care =)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.