after login redirection code?

Discussion in 'Programming & Software Development' started by ausghostdog, May 11, 2012.

  1. ausghostdog

    ausghostdog Member

    Joined:
    Feb 7, 2010
    Messages:
    2,150
    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
     
  2. rm -rp ./Matt*

    rm -rp ./Matt* Member

    Joined:
    Sep 25, 2008
    Messages:
    621
    Location:
    in the computer
    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. OP
    OP
    ausghostdog

    ausghostdog Member

    Joined:
    Feb 7, 2010
    Messages:
    2,150
    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. rm -rp ./Matt*

    rm -rp ./Matt* Member

    Joined:
    Sep 25, 2008
    Messages:
    621
    Location:
    in the computer

    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. OP
    OP
    ausghostdog

    ausghostdog Member

    Joined:
    Feb 7, 2010
    Messages:
    2,150
    Well it worked, no other issues.
     
  6. n000b

    n000b Member

    Joined:
    Jul 4, 2002
    Messages:
    2,489
    Location:
    Melbourne
    Looks up ob_start, and also ob_end_flush.
     
  7. mkshft

    mkshft Member

    Joined:
    Aug 16, 2010
    Messages:
    54
    Nice find, good to know. Thanks :thumbup:

    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. :rolleyes:
     
  8. OP
    OP
    ausghostdog

    ausghostdog Member

    Joined:
    Feb 7, 2010
    Messages:
    2,150
    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. rm -rp ./Matt*

    rm -rp ./Matt* Member

    Joined:
    Sep 25, 2008
    Messages:
    621
    Location:
    in the computer
    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.
     
  10. perrymitchell

    perrymitchell Member

    Joined:
    Jan 1, 2007
    Messages:
    724
    Location:
    Brisbane Southside
    Well it shouldn't work, seeing as header needs to precede any output, as mentioned on php.net:

    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.
     
  11. OPM881

    OPM881 Member

    Joined:
    Apr 21, 2009
    Messages:
    1,652
    Location:
    Cairns
    Why not use a javascript function to re-direct you after a certain amount of time?
     

Share This Page