Jump to content

Recommended Posts

I use this same code on my laptop, it works fine, but I upload it to my web host, and it doesn't work.

 

                while($row = mysql_fetch_array($result))
                  {
                    If ($row['Agent_Password']==$PassWord)
                	{

				 echo "Test";
				 header('Location: agent_mainmenu.php');
				 exit;
                	}
                	Else
                	{
                	 $Error = "Password and/or Username invalid";
                	}
                  }

 

 

Any Ideas?

 

I see the word "test" appear, and the rest of the page doesn't load and it doesn't redirect.

Link to comment
https://forums.phpfreaks.com/topic/168686-solved-header-redirect-doesnt-work/
Share on other sites

you can't echo anything if you are going to do a header redirect.

try this:

               

while($row = mysql_fetch_array($result))
                  {
                    If ($row['Agent_Password']==$PassWord)
                   {
               
                //echo "Test";
                header('Location: agent_mainmenu.php');
                exit;
                   }
                   Else
                   {
                    $Error = "Password and/or Username invalid";
                   }
                  }

you can't echo anything if you are going to do a header redirect.

try this:

 

Heh, that doesn't have anything to do with it.  That was just a test to see where my code was dying.  Commenting that line out doesn't make the redirect work.  The code simply does not redirect, and the page dies right there.

 

Again, it works fine on my laptop (with IIS, and PHP 5, and mySQL installed).  My local system basically mirrors my web host, thus why I'm so befuddled!

 

But... I am also new to php (an ASP convert), so... I have to bow to other people's knowledge for a bit.

 

Here is the code:

 

<?php
If (isset($_GET["logout"]))
{
  setcookie("AgentName","",time()-3600);
  setcookie("AgentLogin","",time()-3600);
  setcookie("AgentEmail","",time()-3600);
  setcookie("AgentVass","",time()-3600);
  header('Location:agent_login.php');
  exit();
}
?>
<?php include("components/inc_header.php"); ?>
<?php include("components/inc_header2.php"); ?>
			<!-- Begin Main Content -->
                <?php
                if (isset($_POST["fUsername"]))
                {
                $UserName = $_POST["fUsername"];
                $PassWord = $_POST["fPassword"];
                $con = mysql_connect("xxx","xxx","xxx");
                if (!$con)
                  {
                  die('Could not connect: ' . mysql_error());
                  }
                mysql_select_db(my_db, $con);
                $result = mysql_query("SELECT * FROM Agents WHERE Agent_Email='$UserName'");
                while($row = mysql_fetch_array($result))
                  {
                    If ($row['Agent_Password']==$PassWord)
                	{
				 setcookie("AgentName",$row['Agent_Name'],time()+3600*24);
				 setcookie("AgentLogin","Agent_5487_Meir",time()+3600*24);
				 setcookie("AgentEmail",$UserName,time()+3600*24);
				 setcookie("AgentVass",$PassWord,time()+3600*24);
				 echo "Page seems to just die right here, no redirect!";

				 header('Location: agent_mainmenu.php');
				 exit;
                	}
                	Else
                	{
                	 $Error = "Password and/or Username invalid";
                	}
                  }
                mysql_close($con);
                }
                ?> 
			<form action="agent_login.php" method="post">
			<table cellspacing="0" cellpadding="4" border="0">
			<tr valign="top">
				<td class="adtext" rowspan="4">    </td>
				<td colspan="2"><h1>Agent Login:</h1></td>
			</tr>
			<tr>
				<td>Username:</td><td><input type="text" name="fUsername" value="<?php if (isset($_COOKIE['AgentEmail'])) echo $_COOKIE['AgentEmail'] ?>"></input></td>
			</tr>
			<tr>
				<td>Password:</td><td><input type="password" name="fPassword" value="<?php if (isset($_COOKIE['AgentVass'])) echo $_COOKIE['AgentVass'] ?>"></input></td>
			</tr>
			<tr>
				 <td colspan="2" align="center"><input type="submit" value="Submit"></input><p><span class="error"><?php if (isset($Error)) echo $Error; ?></span></td>
			</tr>
			<tr><td colspan="2" align="center"><a href="agent_login.php?logout=true">Log Out</a></td></tr>
			</table>
			</form>
			<!-- End Main Content -->

do components/inc_header.php and components/inc_header2.php output anything? I know I'm beating a dead horse, but if *anything* is outputted, it won't do a header redirect. try this from your hosting server:

<?php
header("location:http://google.com");
exit();
?>

if it redirects, then it's not the hosting server, its the code.

The headers are just HTML code (that's use on every page), nothing else.  Just saving lines of code by including it.  Then if I edit the include, it fixes ALL the pages.

 

Ok, you win.  The above code with nothing else worked fine.  :D

 

So... uh...  :facewall:

 

I don't know it works fine locally.  *shrug*

 

Any suggestions on how to accomplish the same thing a different way?

put your check before your header:

<?php
If (isset($_GET["logout"]))
{
setcookie("AgentName","",time()-3600);
setcookie("AgentLogin","",time()-3600);
setcookie("AgentEmail","",time()-3600);
setcookie("AgentVass","",time()-3600);
header('Location:agent_login.php');
exit();
}
if (isset($_POST["fUsername"]))
{
$UserName = $_POST["fUsername"];
$PassWord = $_POST["fPassword"];
$con = mysql_connect("xxx","xxx","xxx");
if (!$con)
{
	die('Could not connect: ' . mysql_error());
}
mysql_select_db(my_db, $con);
$result = mysql_query("SELECT * FROM Agents WHERE Agent_Email='$UserName'");
while($row = mysql_fetch_array($result))
{
	If ($row['Agent_Password']==$PassWord)
	{
		setcookie("AgentName",$row['Agent_Name'],time()+3600*24);
		setcookie("AgentLogin","Agent_5487_Meir",time()+3600*24);
		setcookie("AgentEmail",$UserName,time()+3600*24);
		setcookie("AgentVass",$PassWord,time()+3600*24);
		header('Location: agent_mainmenu.php');
		exit;
	}
	Else
	{
		$Error = "Password and/or Username invalid";
	}
}
mysql_close($con);
}
                ?>
<?php include("components/inc_header.php"); ?>
<?php include("components/inc_header2.php"); ?>
            <!-- Begin Main Content -->
                
            <form action="agent_login.php" method="post">
            <table cellspacing="0" cellpadding="4" border="0">
            <tr valign="top">
               <td class="adtext" rowspan="4">    </td>
               <td colspan="2"><h1>Agent Login:</h1></td>
            </tr>
            <tr>
               <td>Username:</td><td><input type="text" name="fUsername" value="<?php if (isset($_COOKIE['AgentEmail'])) echo $_COOKIE['AgentEmail'] ?>"></input></td>
            </tr>
            <tr>
               <td>Password:</td><td><input type="password" name="fPassword" value="<?php if (isset($_COOKIE['AgentVass'])) echo $_COOKIE['AgentVass'] ?>"></input></td>
            </tr>
            <tr>
                <td colspan="2" align="center"><input type="submit" value="Submit"></input><p><span class="error"><?php if (isset($Error)) echo $Error; ?></span></td>
            </tr>
            <tr><td colspan="2" align="center"><a href="agent_login.php?logout=true">Log Out</a></td></tr>
            </table>
            </form>
            <!-- End Main Content -->
            
            

EDIT*

Forgot to remove the echo

I did as you suggested and it didn't work initially.  So I took your google code, pasting it at the top, and it worked fine. 

 

Then I decided to clear out IE8 cache, put my code back in, and added the cookies back in.  Everything works fine again!

 

Thank you for your expertise, you saved me hours of time!!!!

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.