-1
<?php

$connection = mysql_connect("localhost", "root", "");

$db = mysql_select_db("tumy", $connection);
session_start();

$user_check=$_SESSION['username'];

$ses_sql=mysql_query("select username from customer where   username='$user_check'", $connection);
$row = mysql_fetch_assoc($ses_sql);
$login_session =$row['username'];
//if(!isset($_SESSION['username'])){
//header('Location: homepage.php');
//}

?>

May I know is there need to set any condition when public visiting my website?
Because it shows

undefined index: username in $user_check.

When the header Location: homepage.php, it direct come out with blank page.

So may I know the solution for it?

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
Lim Keong
  • 1
  • 2

2 Answers2

0

I think you need a little help with the logic / process. This would be my suggested method using your code:

// always start this first
session_start();

// check for logged-in user..
if(!isset($_SESSION['username'])){

    // ..and redirect if necessary
    header('Location: homepage.php');
    exit;

} else {

    // only do a db connection if needed
    $connection = mysql_connect("localhost", "root", "");
    $db = mysql_select_db("tumy", $connection);

    $user_check = $_SESSION['username'];

    $ses_sql=mysql_query("select username from customer where   username='$user_check'", $connection);
    $row = mysql_fetch_assoc($ses_sql);

    if($row['username'] != ''){
        $_SESSION['username'] = $row['username'];
    }

    // redirect if required.. ..or continue

}

I hope this helps.

MaggsWeb
  • 3,018
  • 1
  • 13
  • 23
0
session_start();  must be on top of the file.

and you can use the condition

$user_check = isset($_SESSION['username'])?$_SESSION['username']:"";
dev.php
  • 96
  • 5