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>©2nd Handers 2009 All Rights Reserved.</p>
</div>
</body>
</html>
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>©2nd Handers 2009 All Rights Reserved.</p>
</div>
</body>
</html>