TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Login and redirect after. (http://www.talkphp.com/absolute-beginners/3062-login-redirect-after.html)

Peuplarchie 07-03-2008 08:29 PM

Login and redirect after.
 
Good day to you all,
I have a login script which I would like the good user to be redirected.
I know how to redirect a page but I don't know how to make it redirect to a after the login process.


PHP Code:



<?

$u 
$_POST['username']; //username from form
$p $_POST['password']; // password from form.

if($u == "user1" && $p == "pass" ||
   
$u == "user2" && $p == "pass" ||
   
$u == "user3" && $p == "pass"){
  
      
$_SESSION['user'] = $u;
      
$_SESSION['logged'] = "yes"
      
header("location:auth.php");
     
exit;
      }else{

      print 
"username or pass was incorrect";
   
     }
     
     
     
?>



Can somebody help me ?
Thanks !

codefreek 07-03-2008 08:38 PM

PHP Code:

if ($_SESSION['logged'] == 1) { // User is already logged in.

        
header("Location: settourpagename.php"); // Goes to what page u want

        
exit(); // Stops the rest of the script.

} else {

if ( ! isset(
$username))
{
    
$username '';
}

if ( ! isset(
$password))
{
    
$password '';
}

echo 
"Put something here!";



Tanax 07-03-2008 10:39 PM

Actually, your script already works..?
It redirects IF the login was correct, otherwise it will print out that the username or password was incorrect.

Edit:

Ah, you want the logged in users to be redirected from login.php if they try to access it while logged in?
If so, you just do at the top of the script:
php Code:
<?

if($_SESSION['logged'] != 'yes') {

$u = $_POST['username']; //username from form
$p = $_POST['password']; // password from form.

if($u == "user1" && $p == "pass" ||
   $u == "user2" && $p == "pass" ||
   $u == "user3" && $p == "pass"){
 
      $_SESSION['user'] = $u;
      $_SESSION['logged'] = "yes"
      header("location:auth.php");
     
exit;
      }else{

      print "username or pass was incorrect";
   
     }
}

else {

header("Location: index.php");

}

     ?>

Obviously you edit the index.php to w.e page you want them to end up..

codefreek 07-03-2008 11:04 PM

good luck ;)

Tanax 07-04-2008 12:25 AM

Quote:

Originally Posted by codefreek (Post 16561)
Waste, posting 2 times same thing Tx, just with better style ;P

Actually, your script won't even work.

First of all, you check if the session logged is set to (int) 1. When he logged in, the session logged is set to (string) yes, so checking if it's set to 1, will always return FALSE.

Secondly, you check if $username is set, but he don't even have that variable, he uses $u and $p for username and password. So it's the same thing here, this will always return FALSE.

Lastly, I don't even get the point in checking your $username. You check to see if it's NOT set(=empty), and if it fullfills that requirement, you set the $username var to.. NOTHING, meaning.. you don't change the value at all.

codefreek 07-04-2008 01:08 PM

i posted a code i know that the vars are not the same but hey man it is him who need to fix it i just posted a sample..

but hey he can do how he wants ;)
i am just trying to help..


All times are GMT. The time now is 11:07 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0