![]() |
![]() OCAU News - Wiki - QuickLinks - Pix - Sponsors |
|
|||||||
| Notices |
|
Sign up for a free OCAU account and this ad will go away! Search our forums with Google: |
![]() |
|
|
Thread Tools |
|
|
#1 |
|
Member
Join Date: Feb 2010
Posts: 1,138
|
I need help with a bit of code, after a user logs in page. I would like them to be redirected to the index page, at the moment it just stays on the process page with a short message saying they have logged in and if I manually go to the index page it shows them logged in
I have tried Code:
<?php header( 'location: ../index.php') ; ?> my full code for the process log on page is Code:
<?php
session_start();
require_once "../includes/functions.php";
require_once "../database/dbconn.php";
$username = $_POST['username'];
$password = $_POST['password'];
$error_message = null;
$error_message .= validate_username($username);
$error_message .= validate_password($password);
if ($error_message)
die(include_once "login.php");
else
{
$susername = sanitiseMySQL($username);
$spassword = sanitiseMySQL($password);
//$_SESSION['username'] = $susername;
$hspassword = hash ('md5', $spassword);
$query = "SELECT * from tblstaff where username = '$susername' and password = '$hspassword'";
$result = mysql_query($query)or die(mysql_error());
if (!$result)
echo "testing";
else
{
$rows = mysql_num_rows ($result);
if ($rows <1)
{
$error_message = "username or password is wrong";
include_once "login.php";
}
else
{
$_SESSION['logged'] = true;
$row = mysql_fetch_row($result);
$_SESSION['firstname'] = $row[1];
echo"<p> welcome back " . $_SESSION['firstname'] ." </p>";
echo "<p> you are now logged on</p>";
//workedfine to here, getting redirection working.
}
}
}
?>
|
|
|
|
| Join OCAU to remove this ad! |
|
|
#2 |
|
Member
Join Date: Sep 2008
Location: in the computer
Posts: 621
|
you can't do a redirect if something has been outputted to the screen already, in this case your echo statements. You can either omit the echo statements or do a js redirect instead of a php one.
I'd put brackets around all your require statements and {} around your if statements too. |
|
|
|
|
|
#3 |
|
Member
Join Date: Feb 2010
Posts: 1,138
|
worked it out, code I needed was
Code:
{
$_SESSION['logged'] = true;
$row = mysql_fetch_row($result);
$_SESSION['firstname'] = $row[1];
echo"<p> welcome back " . $_SESSION['firstname'] ." </p>";
echo "<p> you are now logged on</p>";
header("Location: ../");
}
|
|
|
|
|
|
#4 | |
|
Member
Join Date: Sep 2008
Location: in the computer
Posts: 621
|
Quote:
You sure that works, I get "Warning: Cannot modify header information - headers already sent by" it's not meant to work that way. Also if you can use mysql_fetch_assoc, then you can get the name as $_SESSION['firstname'] = $row['firstname_in_table']; Then if the table structure changes it won't break, also easier to read. |
|
|
|
|
|
|
#5 | |
|
Member
Join Date: Feb 2010
Posts: 1,138
|
Quote:
|
|
|
|
|
|
|
#6 | |
|
Member
Join Date: Jul 2002
Location: Melbourne
Posts: 2,492
|
Quote:
|
|
|
|
|
|
|
#7 | |
|
Member
Join Date: Aug 2010
Posts: 54
|
Quote:
I am disappointed you're leaving it there. If you want to be a better programmer you should focus more on code quality than rushing to a release. It's not like it's a hard or long fix to make anyway.
|
|
|
|
|
|
|
#8 |
|
Member
Join Date: Feb 2010
Posts: 1,138
|
I'm not leaving it there, I have a number of other things to work through first. I have a sheet with all the bug fixes I wanna clean up at a later point.
|
|
|
|
|
|
#9 | |
|
Member
Join Date: Sep 2008
Location: in the computer
Posts: 621
|
Quote:
|
|
|
|
|
|
|
#10 | |
|
Member
Join Date: Jan 2007
Location: Brisbane Southside
Posts: 679
|
Well it shouldn't work, seeing as header needs to precede any output, as mentioned on php.net:
Quote:
1. The redirect somehow works and you don't see your "success" message (or at least not for very long - This time will change depending on where your users are... not something you want to leave to chance/timing). 2. The page breaks because you're sending display output before the last bit of header data is sent. Header redirects need to occur before any data is sent, see here: http://php.net/manual/en/function.header.php.
__________________
COMP: Phenon II X6 1090T Thuban | Xigmatek Achilles | ASUS M4A89GTD Pro | 12gb DDR3 | 300gb Velociraptor | 3x 1TB Samsung SATA II | Gigabyte GTX 560Ti 1024mb | 20" BenQ & 20" ViewSonic LAPTOP: Dell Inspiron 6400 | 2.5gb DD2-533 | 320gb NAS: Synology DS 1812+ 8x 3TB Seagate Black SATA3 HDDs @ RAID6 16TB SNES Conversion: NTSC Super Nintendo -> Windows 7 Hyperspin box (thread) |
|
|
|
|
|
|
#11 |
|
Member
Join Date: Apr 2009
Location: Cairns
Posts: 1,431
|
Why not use a javascript function to re-direct you after a certain amount of time?
|
|
|
|
![]() |
| Bookmarks |
|
Sign up for a free OCAU account and this ad will go away! |
| Thread Tools | |
|
|