Reputation: 25
I have a bit of a problem, sure one of you experts can assist :-p.
I have the following tables in the database:
b_admins:
id
username
password
email
locationid
b_locations
id
name
description
Login Script:
<?php
// Inialize session
session_start();
// Connection to MySQL Database.
include ('_includes/_dbconnection.php');
include ('_includes/_dbopen.php');
// Retrieve username and password from database according to user's input
$login = mysql_query("SELECT * FROM b_admins WHERE (ausername = '" . mysql_real_escape_string($_POST['username']) . "') and (apassword = '" . mysql_real_escape_string($_POST['password']) . "')");
// Check username and password match
if (mysql_num_rows($login) == 1) {
// Set username session variable
$_SESSION['username'] = $_POST['username'];
// Set locationid session varaible //// Modified 12 April 2012.
$_SESSION['locationid'] = $row_Recordset1["locationid"];
$locationid = $_SESSION['locationid'];
// Jump to secured page
header('Location: _home.php');
}
else {
// Jump to login page
header('Location: index.php');
}
// Script Source: http://frozenade.wordpress.com/2007/11/24/how-to-create-login-page-in-php-and-mysql-with-session/
?>
My homepage script:
// Check, if user is already login, if login proceed to _home.php
if (isset($_SESSION['username'])) {
header('Location: _home.php');
}
// Load Header file.
include ('_includes/_header.php');
// Template Construction.
// Create Homepage Layout after $home01.
$home01 = '<div id="imglogo"></div>
<div id="container">
<div class="top">
<div id="logo">
</div>
<ul class="socialicons">
</ul>
</div>
<div id="content" >
<div id="home">
<div class="latest">';
// MySQL Load Location Name from MySQL database after $home02.
$home02 = '<h3>';
// Create Notification Area for the Administrator after $home03.
$home03 = ' Administrator Panel</h3><p>You are logged in to make changes to your Branch, only your Branch details can be changed within this Administration Panel. You may proceed to make any changes using the menu at the bottom.</p></div>
<!-- /Latest section -->
<!-- News info section -->
<ul class="news-info">
<li><label>Users Registered</label>';
//MySQL Load User count from MySQL database after $home04.
$home04 = '<span>';
// Close Template.
$home05 = '</span></li>
</ul>
<!-- /News info section -->
</div>
<!-- /Home -->
<!-- Menu -->
<div class="menu">
<ul class="tabs">
<li><a href="_home.php" class="tab-home"></a></li>
<li><a href="_events.php" class="tab-events"></a></li>
<li><a href="_gallery.php" class="tab-portfolio"></a></li>
<li><a href="_template_edit.php" class="tab-contact"></a></li>
</ul>
</div>
</div>';
////////////////////////////////////////////////////////////////////////// Combining Template with MySQL Data.
/// Build Home Page from MySQL database.
$SQL = "SELECT * FROM b_admins, b_locations WHERE locationid ='$locationid' LIMIT 0, 1";
$result = mysql_query($SQL);
while ($db_field = mysql_fetch_assoc($result)) {
echo $home01;
echo $home02;
print $db_field['lcity'];
echo $home03;
}
/// Build Home Page News Section from MySQL database.
echo $home04;
$SQL = "SELECT count( * ) as total_record FROM `b_users` WHERE locationid = '$locationid'";
echo $home05;
/// Load Footer and Database close file.
include ('_includes/_footer.php');
include ('_includes/_dbclose.php');
?>
I would like to save the value 'locationsid' from b_admins to be able to use the SQL to retrieve data from the database where the 'locationsid' is the same as from the table in the database b_locations.
I also receive the following error that i have no clue on how to fix when logged in though the login script: "The webpage at http://localhost/xxxxx/_home.php has resulted in too many redirects."
Thanks in advance :-)
Upvotes: 0
Views: 1203
Reputation: 86
seems like you need to fetch the record first you will need to fetch the Mysql result to get the record
$row_Recordset1 = mysql_fetch_array($login, MYSQL_ASSOC);
$_SESSION['locationid'] = $row_Recordset1["locationid"];
also the homepage also needs a session_start(); or it will never be valid as no session exists on load of page only after function call
if you still end up with header redirects you could use html meta refresh also watch you don't point redirects to same page
hope it helps
Upvotes: 1
Reputation: 1232
i think your query is also wrong.
// Retrieve username and password from database according to user's input
$login = mysql_query("SELECT * FROM b_admins WHERE (ausername = '" .mysql_real_escape_string($_POST['username']) . "') and (apassword = '" . mysql_real_escape_string($_POST['password']) . "')");
There's a typo in your code.
ausername should be username
*apassword* should be passsword
Upvotes: 0
Reputation: 41
When you say "homepage script" is this file index.php or _home.php?
If it is _home.php, you do not need the following lines:
// Check, if user is already login, if login proceed to _home.php
if (isset($_SESSION['username'])) {
header('Location: _home.php');
}
This could be what is causing the redirect loop.
Upvotes: 0