Overclockers Australia Forums
OCAU News - Wiki - QuickLinks - Pix - Sponsors  

Go Back   Overclockers Australia Forums > Software Topics > Programming & Software Development

Notices


Sign up for a free OCAU account and this ad will go away!
Search our forums with Google:
Reply
 
Thread Tools
Old 11th May 2012, 8:33 AM   #1
ausghostdog Thread Starter
Member
 
Join Date: Feb 2010
Posts: 1,859
Default after login redirection code?

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') ;
	?>
It did not work

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.
			
						
		}
	}
}

?>
Any help would be loved. I would like them to see the message for a short time (Maybe 5-10 seconds before being moved on to the index.php page
ausghostdog is offline   Reply With Quote

Join OCAU to remove this ad!
Old 11th May 2012, 12:22 PM   #2
rm -rp ./Matt*
Member
 
rm -rp ./Matt*'s Avatar
 
Join Date: Sep 2008
Location: in the computer
Posts: 621
Default

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.
rm -rp ./Matt* is offline   Reply With Quote
Old 11th May 2012, 2:26 PM   #3
ausghostdog Thread Starter
Member
 
Join Date: Feb 2010
Posts: 1,859
Default

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: ../");
}
ausghostdog is offline   Reply With Quote
Old 11th May 2012, 3:15 PM   #4
rm -rp ./Matt*
Member
 
rm -rp ./Matt*'s Avatar
 
Join Date: Sep 2008
Location: in the computer
Posts: 621
Default

Quote:
Originally Posted by ausghostdog View Post
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: ../");
}

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.
rm -rp ./Matt* is offline   Reply With Quote
Old 11th May 2012, 4:54 PM   #5
ausghostdog Thread Starter
Member
 
Join Date: Feb 2010
Posts: 1,859
Default

Quote:
Originally Posted by rm -rp ./Matt* View Post
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.
Well it worked, no other issues.
ausghostdog is offline   Reply With Quote
Old 11th May 2012, 5:13 PM   #6
n000b
Member
 
n000b's Avatar
 
Join Date: Jul 2002
Location: Melbourne
Posts: 2,489
Default

Quote:
Originally Posted by rm -rp ./Matt* View Post
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.
Looks up ob_start, and also ob_end_flush.
n000b is offline   Reply With Quote
Old 12th May 2012, 1:10 PM   #7
mkshft
Member
 
mkshft's Avatar
 
Join Date: Aug 2010
Posts: 54
Default

Quote:
Originally Posted by n000b View Post
Looks up ob_start, and also ob_end_flush.
Nice find, good to know. Thanks

Quote:
Originally Posted by ausghostdog View Post
Well it worked, no other issues.
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.
mkshft is offline   Reply With Quote
Old 13th May 2012, 1:51 PM   #8
ausghostdog Thread Starter
Member
 
Join Date: Feb 2010
Posts: 1,859
Default

Quote:
Originally Posted by mkshft View Post
Nice find, good to know. Thanks



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.
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.
ausghostdog is offline   Reply With Quote
Old 14th May 2012, 10:32 AM   #9
rm -rp ./Matt*
Member
 
rm -rp ./Matt*'s Avatar
 
Join Date: Sep 2008
Location: in the computer
Posts: 621
Default

Quote:
Originally Posted by n000b View Post
Looks up ob_start, and also ob_end_flush.
yeh I use those to output echo statements during long jobs to see updates in realtime, using them in this scenario to avoid a php error doesn't really sound like good practice though.
rm -rp ./Matt* is offline   Reply With Quote
Old 18th May 2012, 3:34 PM   #10
perrymitchell
Member
 
perrymitchell's Avatar
 
Join Date: Jan 2007
Location: Brisbane Southside
Posts: 709
Default

Quote:
Originally Posted by ausghostdog View Post
Well it worked, no other issues.
Well it shouldn't work, seeing as header needs to precede any output, as mentioned on php.net:

Quote:
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.
1 of 2 things will happen:
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)
perrymitchell is offline   Reply With Quote
Old 19th May 2012, 10:46 AM   #11
OPM881
Member
 
Join Date: Apr 2009
Location: Cairns
Posts: 1,585
Default

Why not use a javascript function to re-direct you after a certain amount of time?
__________________
Quote:
Originally Posted by OCAC View Post
I took my friendzone gf to the strippers the other night, she loved it...it's just a show nothing to get upset over - i don't like women who are too conservative.
OPM881 is offline   Reply With Quote
Reply

Bookmarks

Sign up for a free OCAU account and this ad will go away!

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +10. The time now is 11:04 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd. -
OCAU is not responsible for the content of individual messages posted by others.
Other content copyright Overclockers Australia.
OCAU is hosted by Internode!