Jump to content

page not redirecting using header


SUNIL16

Recommended Posts

Hi Friends

 

Below is my code for login authentication

if ($_POST['Submit']=='Login')
{
$username = mysql_real_escape_string($_POST['uname']);
$md5pass = md5($_POST['password']);
$sql = "SELECT usersid,username FROM users WHERE 
            username = '$username' AND 
            password = '$md5pass' AND user_activated='1'"; 

$result = mysql_query($sql) or die (mysql_error()); 
$num = mysql_num_rows($result);

    if ( $num != 0 ) { 

        // A matching row was found - the user is authenticated. 
       session_start(); 
   list($user_id,$usernamel) = mysql_fetch_row($result);
	// this sets variables in the session 
	$_SESSION['user']= $username;  


	if (isset($_GET['ret']) && !empty($_GET['ret']))
	{
	header("Location: $_GET[ret]");
	} else
	{
	header("Location: http://www.xyz.com/myaccount.php");
	}
	//echo "Logged in...";
	exit();
    } 

header("Location: login.php?msg=Invalid Login");
echo "Error:";
exit();		
}

 

I am not getting redirect to myaccount.php using header function.

 

please help..

 

any header functions are not functioning in my pages..

Link to comment
https://forums.phpfreaks.com/topic/146269-page-not-redirecting-using-header/
Share on other sites

So what does happen?

 

Are you sure that you named your submit button with a capital S and the value with a capital L? I ask since none of your other varaiables/array indexes are using capitals...

 

Do you have error_reporting set to E_ALL and display_errors turned on?

I just tried:

 

<?php
if (true)
{
$username = mysql_real_escape_string($_POST['uname']);
$md5pass = md5($_POST['password']);
$sql = "SELECT usersid,username FROM users WHERE
            username = '$username' AND
            password = '$md5pass' AND user_activated='1'";
         
//$result = mysql_query($sql) or die (mysql_error());
//$num = mysql_num_rows($result);

    if ( true ) {

        // A matching row was found - the user is authenticated.
       session_start();
      list($user_id,$usernamel) = mysql_fetch_row($result);
      // this sets variables in the session
      $_SESSION['user']= $username; 
      
         
      if (isset($_GET['ret']) && !empty($_GET['ret']))
      {
      header("Location: $_GET[ret]");
      } else
      {
      header("Location: http://www.yahoo.com");
      }
      //echo "Logged in...";
      exit();
    }

header("Location: http://google.com");
echo "Error:";
exit();      
}
?>

 

and worked. I was redirected to yahoo.com, and if the second true is changed to false I will get redirected to google.com

 

is

$_POST['Submit']=='Login'

 

being set? (does it returns true?)

Thanks for reply to both of you,

 

below is my form code

<form name="myform" action="login.php" method="post" onSubmit="return validate()">

<table class="main_txt" border="1"  width="100%">

  <tr>

    <td width="595" colspan="2" bgcolor="#BFBFBF" ><font color="#FFFFFF"><b>Login</b></font></td>

  </tr>

  <tr>

    <td width="292" bgcolor="#EBEBEB" >User Name</td>

    <td width="301" bgcolor="#EBEBEB" > <input type="text" name="uname" size="20" maxlenght="30"></td>

  </tr>

  <tr>

    <td width="292" bgcolor="#EBEBEB" >Password</td>

    <td width="301" bgcolor="#EBEBEB" > <input type="password" name="password" size="20" maxlenght="10"></td>

  </tr>

  <tr>

    <td width="595" colspan="2" bgcolor="#BFBFBF" height="1">                                  

      <input type="Submit" name="Submit" value="Login"> (<a href="forgotpass.html">Forgot password</a>)</td>

  </tr>

</table>

</form>

 

I used caps only.

 

and $_POST['Submit']=='Login' is setting but still i am not getting.

 

Is there anything like header should be used at the top of all other code?

Do you have error_reporting set to E_ALL and display_errors turned on?

You did not answer that question. About 99.98 percent of the time developing and debugging your code on a system with those two settings the php errors will tell you why a header does not work.

hi its working fine only that is if you echo"logged in" in place header function its showing as logged in.

 

what you said is write you got a exit; and it will show on a page on it own any think that above the exit function.

 

 

 

 

 

 

Hi Friends,

 

i even tried by giving below code in my php file

 

ini_set(’display_errors’, ‘On’);

ini_set(’error_reporting’, E_ALL);

 

But still its not redirecting....

please give any suggestions. i am totally confused. waiting for reply.

i given redirection to www.yahoo.com but still its not working.

 

here i am posting the entire code once again. Please look into it and let me know where i went wrong..

 

<h1 class="h1" align="left"> Login</h1>

<?php

require "config.php";

 

if ($_POST['Submit']=='Login')

{

$username = mysql_real_escape_string($_POST['uname']);

$md5pass = md5($_POST['password']);

$sql = "SELECT usersid,username FROM users WHERE

            username = '$username' AND

            password = '$md5pass' AND user_activated='1'";

 

$result = mysql_query($sql) or die (mysql_error());

$num = mysql_num_rows($result);

 

    if ( $num != 0 ) {

 

        // A matching row was found - the user is authenticated.

      session_start();

  list($user_id,$usernamel) = mysql_fetch_row($result);

// this sets variables in the session

$_SESSION['user']= $username; 

 

 

 

if (isset($_GET['ret']) && !empty($_GET['ret']))

{

header("Location: $_GET[ret]");

} else

{

header("Location: http://www.yahoo.com");

}

//echo "Logged in...";

exit();

    }

 

header("Location: login.php?msg=Invalid Login");

echo "Error:";

exit();

}

 

?>

 

<form name="myform" action="login.php" method="post" >

<table class="main_txt" border="1"  width="100%">

  <tr>

    <td width="595" colspan="2" bgcolor="#BFBFBF" ><font color="#FFFFFF"><b>Login</b></font></td>

  </tr>

  <tr>

    <td width="292" bgcolor="#EBEBEB" >User Name</td>

    <td width="301" bgcolor="#EBEBEB" > <input type="text" name="uname" size="20" maxlenght="30"></td>

  </tr>

  <tr>

    <td width="292" bgcolor="#EBEBEB" >Password</td>

    <td width="301" bgcolor="#EBEBEB" > <input type="password" name="password" size="20" maxlenght="10"></td>

  </tr>

  <tr>

    <td width="595" colspan="2" bgcolor="#BFBFBF" height="1"> 

      <input type="Submit" name="Submit" value="Login"> (<a href="forgot.php">Forgot password</a>)</td>

  </tr>

</table>

</form>

 

i am not getting header msg any thing with header function in all my pages.

A) You never posted that complete code before,

B) You have HTML content being output on line one of that file that prevents the headers from working,

C) Wherever you put the lines of code for full php error reporting was not in that file because you would have gotten errors telling you where the HTML content was being output.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.