Jump to content

more issues


mrooks1984

Recommended Posts

i am trying to sort out a session now,

 

i normally use the folllowing code to check if someone is logged in and if they are not to forward them to the login page,

but i am getting this message:

 

Notice: Undefined index: username in

 

code:

if ($_SESSION['username']) {
}else{
include 'login.php';
die("");
}

 

can someone help please.

Link to comment
https://forums.phpfreaks.com/topic/249463-more-issues/
Share on other sites

thanks for your responces this is before i even login.

 

the full code is

<?php

//start a seesion
session_start();

// Include Main Core File.
include '../_class/core.php';

// Include Config Files
include '../config/config.php';
include '../config/db.php';

// Use Database
$obj->connect();

if ($_SESSION['username']) {
}else{
include 'login.php';
die("");
}

?>

 

when i do this:

<?php

//start a seesion
session_start();

// Include Main Core File.
include '../_class/core.php';

// Include Config Files
include '../config/config.php';
include '../config/db.php';

// Use Database
$obj->connect();



echo $_SESSION['username'];

?>

 

i get : Notice: Undefined index: username in

 

and no echo of the username

 

could you give me a example of how its checked as well please

 

thanks again.

Link to comment
https://forums.phpfreaks.com/topic/249463-more-issues/#findComment-1280852
Share on other sites

As AyKay47 pointed out, you probably want to use isset() or empty()  but because $_SESSION['username'] won't be set until a user logs in I would use isset(). 

if (isset($_SESSION['username'])) {
}else{
include 'login.php';
die("");
}

The problem I see with this setup (not knowing what login.php includes) is that this more than likely includes a form and the processing code which also sets the "username" to session.  This not only is happening after the "if (isset($_SESSION['username']))" is called but I assume after content, i.e form etc is sent to the browser.

 

I would make a page called login.php that has the form in the page "body"and all processing done before the html tags on this page.  If username, password checks out you set the session.  On all other pages I would have

if (isset($_SESSION['username'])) {
}else{
header ("location:  login.php");
die("");
}

Link to comment
https://forums.phpfreaks.com/topic/249463-more-issues/#findComment-1280924
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.