meow
meow

Reputation: 95

Session Start and Stop Error Php

Index.php

<?php
session_start();
require_once('../inc/db/dbc.php');
include('login_helper.php'); 
?>

<!--

html form

-->

Login/Logout Links depending on session state:
<?php 
    if($_SESSION['valid'] == 1){
        echo "<a href='logout.php'>Logout</a>";
        echo 'userID '.$userid;
    }
    else{
        echo "<a href='index.php'>Login</a>";
    }
?>

check_buyer.php

<?php
session_start(); #recall session from index.php where user logged include()
require_once('login_helper.php');
/*
function validateUser()
{
    session_regenerate_id (); //this is a security measure
    $_SESSION['valid'] == 1;
    $_SESSION['userid'] = $userid;
}
function isLoggedIn()
{
    if(isset($_SESSION['valid']) && $_SESSION['valid'])
        return true;
    return false;
}
*/

require_once('../inc/db/dbc.php');
$connect = mysql_connect($h, $u, $p) or die ("Can't Connect to Database.");
mysql_select_db($db);

$LoginUserName = $_POST['userName'];
$LoginPassword = mysql_real_escape_string($_POST['userPass']);
//connect to the database here
$LoginUserName = mysql_real_escape_string($LoginUserName);
$query = "SELECT uID, uUPass, dynamSalt
        FROM User
        WHERE uUName = '$LoginUserName';";
$result = mysql_query($query);
if(mysql_num_rows($result) < 1) //no such USER exists
{
    echo "Invalid Username and/or Password";
}
$ifUserExists = mysql_fetch_array($result, MYSQL_ASSOC);

$dynamSalt = $ifUserExists['dynamSalt'];  #get value of dynamSalt in query above
$SaltyPass = hash('sha512',$dynamSalt.$LoginPassword); #recreate originally created dynamic, unique pass

if($SaltyPass != $ifUserExists['uUPass']) # incorrect PASS
{
    echo "Invalid Username and/or Password";
}

else {
validateUser();
}
//if the user has not logged in
if(!isLoggedIn())
{
    echo "<html>
<head>
<meta http-equiv='refresh' content='0'; url=index.php'>
</head>
<body>
</body>
<html>";
    die();
}

?>

login_helper.php

<?php
 function validateUser()
{
    #session_regenerate_id (); //this is a security measure
    $_SESSION['valid'] == 1;
    $_SESSION['uID'] = $userid;
    echo "Session made";
}

function isLoggedIn()
{
    if(isset($_SESSION['valid']) && $_SESSION['valid'])
        return true;
    return false;
}

function logout()
{
    session_start();
    $_SESSION = array(); //destroy all of the session variables
    session_destroy();
   echo "
    <html>
    <head>
    <meta http-equiv='refresh' content='0'; url=index.php'>
    </head>
    <body>
    </body>
    <html>";
}
?>    

pwhome.php

<?php
session_start();

function isLoggedIn()
{
    if(isset($_SESSION['valid']) && $_SESSION['valid'])
        return true;
    return false;
}

//if the user has not logged in
if(!isLoggedIn())
{
    header('Location: index.php');
    die();
}
?>

<?php 
    if($_SESSION['valid'] == 1){
        echo "<a href='logout.php'>Logout</a>";
        echo 'userID '.$userid;
    }
    else{
        echo "<a href='index.php'>Login</a>";
    }
?>

logout.php

<?php    
require_once('login_helper.php');
logout();
?>

Current State: When I visit index.php and login with credentials that are indeed correct, I get a never ending refresh of check_buyer.php

How do I get this to login in properly (from index.php) and redirect me properly to pwhome.php upon providing valid credentials on index.php ?

Upvotes: 0

Views: 6407

Answers (2)

Arif RH
Arif RH

Reputation: 175

I wonder with your code, if you want to logout and refresh the index.php with new session value, why dont you put header( 'Location: index.php' ); in your logout function?

So, i think this probably will help, modify your logout.php:

Logout.php

<?php  
session_start();
function logout()
{
  $_SESSION = array(); //destroy all of the session variables
  session_destroy();
  echo "logged out?";
  header( 'Location: index.php' );
}

logout();
?>

Last Edited :

Try this codes :

Index.php

<?php
session_start();
require_once('../inc/db/dbc.php');
?>

<!-- 

html form 

-->

<?php 
    if($_SESSION['valid'] == 1){
        echo "<a href='logout.php'>Logout</a>";
        echo 'userID '.$userid;
    }
    else{
        echo "<a href='index.php'>Login</a>";
    }
?>

check_buyer.php

<?php
session_start(); #recall session from index.php where user logged include()

require_once('../inc/db/dbc.php');
$connect = mysql_connect($h, $u, $p) or die ("Can't Connect to Database.");
mysql_select_db($db);

$LoginUserName = $_POST['userName'];
$LoginPassword = mysql_real_escape_string($_POST['userPass']);
//connect to the database here
$LoginUserName = mysql_real_escape_string($LoginUserName);
$query = "SELECT uID, uUPass, dynamSalt
        FROM User
        WHERE uUName = '$LoginUserName';";
$result = mysql_query($query);
if(mysql_num_rows($result) < 1) //no such USER exists
{
    echo "Invalid Username and/or Password";
}
$ifUserExists = mysql_fetch_array($result, MYSQL_ASSOC);

$dynamSalt = $ifUserExists['dynamSalt'];  #get value of dynamSalt in query above
$SaltyPass = hash('sha512',$dynamSalt.$LoginPassword); #recreate originally created dynamic, unique pass

if($SaltyPass != $ifUserExists['uUPass']) # incorrect PASS
{
    echo "Invalid Username and/or Password";
}

else {
validateUser();
}
//if the user has not logged in
if(!isLoggedIn())
{
    header('Location: index.php');
    die();
}

function validateUser()
{
    session_regenerate_id (); //this is a security measure
    $_SESSION['valid'] = 1;
    $_SESSION['uID'] = $userid;
}

function isLoggedIn()
{
    if(isset($_SESSION['valid']) && $_SESSION['valid'])
        return true;
    return false;
}
?>

logout.php

<?php    
session_start();
function logout()
{
    $_SESSION = array(); //destroy all of the session variables
    session_destroy();
    header( 'Location: index.php' );
}
logout();
?>

Upvotes: 1

Emir Akaydın
Emir Akaydın

Reputation: 5823

Instead of

header('Location: index.php');

Try meta refresh for page forwarding. After closing the php block, add some HTML code like;

<html>
<head>
<meta http-equiv="refresh" content="0; url=index.php">
</head>
<body>
</body>
<html>

Sometimes session doesn't work as it should when you use header() function for page forwarding.

Upvotes: 0

Related Questions