Page 1 of 1

If logged in...

Posted: Wed Jun 15, 2011 6:06 am
by kalipsso
I made this code for users that are logged in to "Not see the log-in form" when if they are logged in.
It works when i am logged in, but where i log-out, i cant access the log-in form. I am getting this:
The page isn't redirecting properly

Firefox has detected that the server is redirecting the request for this address in a way that will never complete.

This problem can sometimes be caused by disabling or refusing to accept
cookies.

Here is the code:
[syntax=php]if(is_logged_in) {
header("Location: edit_profile.php");
}
else {
header("Location: login.php");
}[/syntax]

What have i done wrong??

i have also made changes in user.inc.php file:
[syntax=php]//checks if given username and password conbination is valid.
function valid_credentials($user, $pass){
$user = mysql_real_escape_string($user);
$pass = mysql_real_escape_string($pass);

//$total = mysql_query("SELECT COUNT('user_id') FROM `users` WHERE `user_name` = '{$user}' AND `user_password` = '{$pass}'");
$total = mysql_query("SELECT * FROM `users` WHERE `user_name` = '{$user}' AND `user_password` = '{$pass}'");
if($total)
{
if(mysql_num_rows($total) > 0)
{
$_SESSION['uid'] = mysql_result($total, 0, "user_id");
//Add session variables here if needed

return true;
}
else
{
return false;
}
}
else
{
return false;
}
//return (mysql_result($total, 0) == '1') ? true : false;
}
[/syntax]

Re: If logged in...

Posted: Wed Jun 15, 2011 6:27 am
by Tino
What is 'is_logged_in'?

The reason for that error is because you are apparently redirecting to a page that will immediately redirect again. For example, if you have

page1.php

[syntax=php]if ( 1 === 1 ) {
header('Location: page2.php');
}[/syntax]

page2.php

[syntax=php]if ( 2 === 2 ) {
header('Location: page1.php');
[/syntax]

you essentially get the same effect as an infinite loop.

Re: If logged in...

Posted: Wed Jun 15, 2011 6:38 am
by kalipsso
Do you have any solution?
Regards and thanks for the fast reply.

Re: If logged in...

Posted: Wed Jun 15, 2011 8:41 am
by kalipsso
OK.
I have found a solution...
[syntax=php]<?php
if (isset($_SESSION['username']) === false){
?>

form here

<?php
}else{
echo "Welcome <a href='profile.php'>" . $_SESSION['username']."</a> ! - <a href='logout.php'>Logout</a>.";
}
?>

[/syntax]
Just in case someone else will need it.
I also modified the profile.php file to display the logged in user profile...