Jump to content

echo and header(location:) conflict


zgkhoo

Recommended Posts

<?php
include 'config.php';
include 'opendb.php';

  function LoginF($username,$password){
  	//echo $username.$password;                    <---cant enable it
  	$result=mysql_query("SELECT * from person");
  	
  	while($row = mysql_fetch_array($result,MYSQL_ASSOC)){

  		
  		if(($username==$row['Username'])&&($password==$row['Password'])){
  			//echo "username success";             <----cant enable it
  			if($row['Level']=='Boss'){
  			header('Location: bossmenu.php');
  			}
  			
  			if($row['Level']=='Agent'){
  			header('Location: agentmenu.php');
  			}  	
  			
  			if($row['Level']=='Customer'){
  			header('Location: customermenu.php');
  			}		
  			
  			if($row['Level']=='Typist'){
  			header('Location: typistmenu.php');
  			}	 		
  		}
  		else {
  		//	echo "invalid login";                               <--cant enable it..
  		}

  		
  	}// end of while loop
  	
  	
  	
  }//end of function LoginF
  



if(isset($_POST['createDB'])){ 
include 'createdb.php';
echo "testing";
}



if(isset($_POST['cPerson'])){ 

echo "cperson";



$sql = "CREATE TABLE person
(
	Username varchar(13), 
	Password varchar(13),
	Level varchar(13)

)";
mysql_query($sql, $con) or die("Can't execute sql" . mysql_error());



$sql="INSERT INTO person (Username,Password,Level)VALUES ('testing1','testing1','Boss')"; mysql_query($sql, $con);
$sql="INSERT INTO person (Username,Password,Level)VALUES ('testing2','testing2','Agent')"; mysql_query($sql, $con);
$sql="INSERT INTO person (Username,Password,Level)VALUES ('testing3','testing3','Customer')"; mysql_query($sql, $con);
$sql="INSERT INTO person (Username,Password,Level)VALUES ('testing4','testing4','Typist')"; mysql_query($sql, $con);

}

  if(isset($_POST['cTrans'])){ 
  	



$sql = "CREATE TABLE transaction
(
	Username varchar(13)

)";
mysql_query($sql, $con) or die("Can't execute sql" . mysql_error());

$sql="INSERT INTO transaction (Username)
	VALUES ('testing')";


mysql_query($sql, $con);

}

   if(isset($_POST['cResult'])){ 


$sql = "CREATE TABLE Result
(
	Username varchar(13)

)";
mysql_query($sql, $con) or die("Can't execute sql" . mysql_error());

$sql="INSERT INTO Result (Username)
	VALUES ('testing')";


mysql_query($sql, $con);

}

  if(isset($_POST['Login'])){
  	//echo "sign in";
  	
  	LoginF($_POST[username],$_POST[password]);

  	
  	
  }

  
?>


<html>
<body>
</br>
</br>
</br>

<center>
<h1>
Sports Toto

</h1>
<img src="toto.gif">
<form action="index.php" method="POST">
<h2>Username

<input type="text" name="username"/>
<br/>
Password
<input type="password" name="password"/>
</h2>
<input type="submit" name="Login" value="Log In">
<button name="createDB" value="createDB33"> create db </button>
<button name="cPerson">Create person table</button>
<button name="cTrans">Create transaction table</button>
<button name="cResult">Create result table</button>


</form>
</center>

</body>
</html>

 

cant enable the echo above, if i  enable it ..will occur error msg..

Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\xampp\Sportstoto\index.php:6) in C:\Program Files\xampp\Sportstoto\index.php on line 15

Link to comment
Share on other sites

You just need to code around it. Your page should be built so that it first performs the logic THEN outputs the HTML.

 

Something like this:

 

<?php
 function LoginF($username,$password){
   $output = $username.$password."\n";
   $result=mysql_query("SELECT * from person");

   while($row = mysql_fetch_array($result,MYSQL_ASSOC)){

     if(($username==$row['Username'])&&($password==$row['Password'])){
       $output = "username success\n";
       if($row['Level']=='Boss'){
         header('Location: bossmenu.php');
       }

       if($row['Level']=='Agent'){
         header('Location: agentmenu.php');
       }  	

       if($row['Level']=='Customer'){
         header('Location: customermenu.php');
       }		

       if($row['Level']=='Typist'){
         header('Location: typistmenu.php');
       }	 		
     }
     else {
       $output = "invalid login\n";
     }

   }// end of while loop

   echo $output;

 }//end of function LoginF
?>

Link to comment
Share on other sites

No thorpe.. He want to make the code to work in this manner.

 

if(redirection fails)

{

then echo the message;

}

 

thats it.

 

Where do you see that? The code posted shows that IF login passes AND the user is of a specified level THEN he wants to redirect. There is nothing to show that the code is attempting to echo if the redirect fails.

 

However, if login fails or if the user is not of any of the specified levels THEN the messages should be echo'd to the page.

Link to comment
Share on other sites

Why would you want to echo something on one page then redirect to another anyway? It makes no sense. Even if it was plausible, the client would never see it.

 

if fail login then display error msg...if successful login then redirect...

 

That is perfectly valid logic.

 

A psuedo example.

<?php

  if ($loginvalid) {
    dologin();
    header('members.php');
  } else {
    echo 'login failed';
  }

?>

 

However, your code does not follow that same logic.

Link to comment
Share on other sites

Where do you see that? The code posted shows that IF login passes AND the user is of a specified level THEN he wants to redirect. There is nothing to show that the code is attempting to echo if the redirect fails.

 

However, if login fails or if the user is not of any of the specified levels THEN the messages should be echo'd to the page.

 

That is what zgkhoo wants to do .. See his post below yours Mr.mjdamato !!

 

http://www.phpfreaks.com/forums/index.php/topic,161014.msg703392.html#msg703392

Link to comment
Share on other sites

Where do you see that? The code posted shows that IF login passes AND the user is of a specified level THEN he wants to redirect. There is nothing to show that the code is attempting to echo if the redirect fails.

 

However, if login fails or if the user is not of any of the specified levels THEN the messages should be echo'd to the page.

 

That is what zgkhoo wants to do .. See his post below yours Mr.mjdamato !!

 

http://www.phpfreaks.com/forums/index.php/topic,161014.msg703392.html#msg703392

 

Ok, I'm not trying to be rude, but you are totally off. You stated

if(redirection fails)

{

then echo the message;

}

 

zgkhoo stated

if fail login then display error msg

 

Those are NOT the same. Login failing and Redirection failing are two different things. Off Topic: can you even test if a redirection fails?

 

@zgkhoo Did you try the code I posted? I think it will do what you want.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.