Log in

View Full Version : Session Login Problems



aakinn
02-18-2010, 12:38 PM
Hello, im fairly new to PHP and im creating a website which requires a user login.

I am able to login but im having problem keeping state when I browse through the pages of the website. My login script is below

LoginDB.php
<?php

//clean up magic quotes

function clean($str) {

$str = @trim($str);

if(get_magic_quotes_gpc()) {

$str = stripslashes($str);

}

return mysql_real_escape_string($str);

}



session_start();



require '/home/ka751/include/mysql.php';

$username = $_POST['myusername'];

$password = $_POST['mypassword'];



if ( !($link=mysql_connect($host, $user, $passwd)) ) {

echo '<p>Error connecting to database</p>';

}

else

mysql_select_db($dbName);



$result = "SELECT * FROM tblLogin WHERE username = '$username' AND pword=password('$password') AND verified ='Y'";

$result = mysql_query ($result) or die (mysql_error());

$row = mysql_fetch_assoc ($result);





if (mysql_num_rows($result) == 1){

$_SESSION['loggedin'] = true;

$_SESSION['username'] = $username;

header ('Location: userprofile.php');

}

else {

header ('Location: loginfailed.php');



}

?>



Once there has been a login I want to change the contents of a div tag but im getting several errors. Here is a sample of my attempted implementation and im getting the following errors

Notice: Undefined index: username in /home/ka751/public_html/WAT/index.php on line 7

Notice: Undefined variable: username in /home/ka751/public_html/WAT/index.php on line 7

Notice: Undefined index: loggedin in /home/ka751/public_html/WAT/index.php on line 9

Index.php
<?php
error_reporting(E_ALL);
session_start();

require '/home/ka751/include/mysql.php';

$username .= $_SESSION["username"];

if ($_SESSION['loggedin'] == true){

$_SESSION['user'] = "<div id='left'>
Welcome $username,
<a href='upload.php' title='Upload Music'>Upload</a> -
<a href='settings.php' title='settings'>Settings</a> -
<a href='logout.php' title='logout'>Logout</a>
</div>";
}
else{
$_SESSION['user'] = "<div id='left'>
<a href='register.php' title='Register with Tunes Inc.'>Register</a> -
<a href='login.php' title='Login with Tunes Inc.'>Login</a>
</div>";
}

?>

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-gb">

<head>

<title>2nd Handers | Home </title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<link rel="stylesheet" type="text/css" href="styles.css" />

<script type="text/javascript" src="loginForm.js"></script>

</head>

<body>

<!-- start main -->

<div id="main">



<!-- start header -->

<div id="header">



<!-- end header -->

</div>



<!-- start nav -->

<div id="navigation">

<ul>

<li class="current"><a href="#">Home</a></li>

<li><a href="products.php">Products</a></li>

<li><a href="login.php">Login</a></li>

<li><a href="register.php">Register</a></li>

<li><a href="about.php">About</a></li>

<li><a href="contact.php">Contact</a></li>

</ul>

<!-- end nav -->

</div>



<!-- start lef -->

<div id="left">


</div>





<!-- start right -->

<div id="right">

<h1>Welcome to 2nd Handers Online</h1><br />

<p>Find CDs to buy and sell online at 2nd Handers. <br />Browse our second hand CDs and buy and sell CDs online at 2ndHanders</p>

<p><img src="images/headphones.jpg" alt="Headphones image"/></p>

<!-- end right -->

</div>



<!-- end main -->

</div>

<div id="footer">

<p>&copy;2nd Handers 2009 All Rights Reserved.</p>

</div>

</body>

</html>

traq
02-18-2010, 05:00 PM
have you tried checking to see if your user info is being saved to the session? "Undefined Index" means PHP can't find a variable you're trying to use.

I'd suggest moving session_start() to the very first line:

<?php
session_start();
// all other code
If anything is output to the browser (e.g., your errors) before the session is called, it won't start.

Also, correct me if I'm wrong, but shouldn't
$username .= $_SESSION["username"]; be
$username = $_SESSION["username"];? Otherwise, you'll end up with $username = "usernameusernameusernameusernameusernameusernameusername" after browsing the site for a time

aakinn
02-18-2010, 05:13 PM
Yeh I noticed that I was appending that variable and changed it just after I posted, but even when the session start is at the top there seems to be the same problem


Notice: Undefined variable: username in /home/ka751/public_html/WAT/index.php on line 14

Notice: Undefined index: loggedin in /home/ka751/public_html/WAT/index.php on line 16


Thanks for your reply

traq
02-18-2010, 06:28 PM
You can use something like this to see all your SESSION variables. (My function pre_var_dump() is just to make it all readable.) Try it and see if your username and loggedin are being saved to your session.


<?php

function pre_var_dump($vars){
echo '<pre>';
var_dump($vars);
echo '</pre>';
}

pre_var_dump($_SESSION);

?>