Jump to content

How to redirect after authentication ??


ashucool83

Recommended Posts

[code]
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="shortcut icon" href="" />
</head>

<body>
<table width="100%" border="0">
  <tr>
    <td><div align="left"><img src="images/myDesiBook_new_2.jpg" width="481" height="100"></div></td>
  </tr>
</table>

<table  width="100%" border="1"> 
    <td width="50%">
</td>
    <td width="50%">
<form action="authenticate.php" method="post" name="login" id="login">
        <p align="left"><em><strong>Please login using your account</strong></em></p>
        <p>Username:
          <input type="text" name="username">
          <br>
          Password:
          <input type="password" name="password">
          <br>
          <input type="submit" value="Login">
          <br>
          If new user please <a href="register.php">Register</a>. </p>
        <p>&nbsp;</p>
        <p>&nbsp;</p>
        <p>&nbsp;</p>
        <p>&nbsp;</p>
        <p>&nbsp; </p>
      </form>
    </td>
</table>

</body>
</html>

[/code]


[code]
<html>
<head>
<title>Library | Login </title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
     
<?php
$db = mysql_connect("localhost", "root", "apurva");


if (!$db)
{
  die('Could not connect: ' . mysql_error());
}
else
{
//echo 'Connected successfully';
}
mysql_select_db("sharelib",$db);
        $varUser = $_POST["username"];
$varPass = $_POST["password"];
$result = mysql_query("SELECT * FROM user_table where email = '".$varUser."'
                      and password =PASSWORD('".$varPass."')",$db);

if($result)
{

if($myrow = mysql_fetch_row($result))
{
print "Authenticated Successfully";
//$URL="http://localhost/MyLibrary/home.php";
//header ('Location: $URL');
//header ('Location: http://localhost/MyLibrary/home.php');
}
else
{
// print "you dont exist";
//Trying to redirect to Login Page
print "Authentication Failure";
$URL="http://localhost/MyLibrary/login.php";
header ("Location: $URL");
exit;
}
}
else
{
print "Oooops DB Syntax Problem!!!";
}

mysql_close($db);
?>

</body>
</html>
[/code]

In the above code, I am trying to authenticate the user from the userTable inside a mySQL DB. I sucessfully able to authenticate the user, but it does not redirect to the desired page after authenticating the user.  For Wrong Password --> "Login.php" Correct Password --> "Home.php"

Please help me.
Link to comment
Share on other sites

Does it print the Authenticated Successfully message? If not, how do you know that authentication is succcesful? If so you won't redirect because your header statement is commented out and when you uncomment it, you won't  be redirected because headers have already been sent via the print.
Link to comment
Share on other sites

The $url in the header vstatement is within single quotes. That doesn't work. If you want to do it like this the header statement must be within double quotes or the url must be concatenated. Like this:
[code]header ("Location: $URL");
or
header ('Location: '. $URL);
[/code]
Link to comment
Share on other sites

I tried this before, as well again after you mentioned it. Still doesnt work  :(
I read some place that HEADER should be the first thing .... but as I am a newbie, didn't quite understand how to do that before performing the authentication logic ???
Link to comment
Share on other sites

[code]<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="shortcut icon" href="" />
</head>

<body>
<table width="100%" border="0">
  <tr>
    <td><div align="left"><img src="images/myDesiBook_new_2.jpg" width="481" height="100"></div></td>
  </tr>
</table>

<table  width="100%" border="1"> 
    <td width="50%">
</td>
    <td width="50%">
<form action="authenticate.php" method="post" name="login" id="login">
        <p align="left"><em><strong>Please login using your account</strong></em></p>
        <p>Username:
          <input type="text" name="username">
          <br>
          Password:
          <input type="password" name="password">
          <br>
          <input type="submit" value="Login">
          <br>
          If new user please <a href="register.php">Register</a>. </p>
        <p>&nbsp;</p>
        <p>&nbsp;</p>
        <p>&nbsp;</p>
        <p>&nbsp;</p>
        <p>&nbsp; </p>
      </form>
    </td>
</table>

</body>
</html>
[/code]


[code]
   
<?php

$db = mysql_connect("localhost", "root", "apurva");

if (!$db)
{
  die('Could not connect: ' . mysql_error());
}
else
{
//echo 'Connected successfully';
}
mysql_select_db("sharelib",$db);
        $varUser = $_POST["username"];
$varPass = $_POST["password"];
$result = mysql_query("SELECT * FROM user_table where email = '".$varUser."'
                      and password =PASSWORD('".$varPass."')",$db);

if($result)
{

if($myrow = mysql_fetch_row($result))
{
//print "Authenticated Successfully";
$URL="http://localhost/MyLibrary/home.php";
//header ('Location: $URL');
//header ('Location: http://localhost/MyLibrary/home.php');
header ("Location: $URL");
}
else
{
// print "you dont exist";
//Trying to redirect to Login Page
//print "Authentication Failure";
$URL="http://localhost/MyLibrary/login.php";
//header ("Location: $URL");
//header ('Location: http://localhost/MyLibrary/login.php');
header ("Location: $URL");
exit;
}
}
else
{
print "Oooops DB Syntax Problem!!!";
}

mysql_close($db);
?>

[/code]
Link to comment
Share on other sites

Yes, I'll post the code exactly as I used it. I onlu changed the db parms and userid/password and I did not use the PASSWORD attrib for the select.
[code]<?php

$db = mysql_connect("localhost", "ronverdonk", "ronnie09");

if (!$db)
{
  die('Could not connect: ' . mysql_error());
}
else
{
//echo 'Connected successfully';
}
mysql_select_db("vwso",$db);
        $varUser = "ronverdonk";
        $varPass = "ronnie09";
$result = mysql_query("SELECT * FROM authorized_users where userid = '".$varUser."'
              and passwd='".$varPass."'");
                     

if($result)
{

if($myrow = mysql_fetch_row($result))
{
//print "Authenticated Successfully";
$URL="http://localhost/MyLibrary/home.php";
//header ('Location: $URL');
//header ('Location: http://localhost/MyLibrary/home.php');
header ("Location: $URL");
}
else
{
// print "you dont exist";
//Trying to redirect to Login Page
//print "Authentication Failure";
$URL="http://localhost/MyLibrary/login.php";
//header ("Location: $URL");
//header ('Location: http://localhost/MyLibrary/login.php');
header ("Location: $URL");
exit;
}
}
else
{
print "Oooops DB Syntax Problem!!!";
}

mysql_close($db);
?>[/code]
Link to comment
Share on other sites

[code]
<?php

$db = mysql_connect("localhost", "root", "apurva");

if (!$db)
{
  die('Could not connect: ' . mysql_error());
}
else
{
//echo 'Connected successfully';
}

mysql_select_db("sharelib",$db);
        $varUser = $_POST["username"];
$varPass = $_POST["password"];
$result = mysql_query("SELECT * FROM user_table where email = '".$varUser."'          
  and password =PASSWORD('".$varPass."')",$db);
                     

if($result)
{

if($myrow = mysql_fetch_row($result))
{
//print "Authenticated Successfully";
$URL="http://localhost/MyLibrary/home.php";
//header ('Location: $URL');
//header ('Location: http://localhost/MyLibrary/home.php');
header ("Location: $URL");
}
else
{
// print "you dont exist";
//Trying to redirect to Login Page
//print "Authentication Failure";
$URL="http://localhost/MyLibrary/login.php";
//header ("Location: $URL");
//header ('Location: http://localhost/MyLibrary/login.php');
header ("Location: $URL");
exit;
}
}
else
{
print "Oooops DB Syntax Problem!!!";
}

mysql_close($db);
?>
[/code]

Working Code!!!  Very weird!!!

Thanks folks :D
Link to comment
Share on other sites

Now that I can go to my home.php page .... do I need to write a cookie to keep my session only for user'A' and not user 'B' cause currently if I type

http://localhost/MyLibrary/home.php

I can still reach the page without the authentication. How do you guys suggest fixing this ?

Thanks
Link to comment
Share on other sites

That is a different story. After authentication you need to save your session in the $_SESSION array. At each page (after the session_start) you verify that the $_SESSION variables are still intact. If not, redirect visitor to the login.php page.
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.