Page 1 of 1

REDIRECTING A PAGE

Posted: Mon Aug 04, 2008 3:41 pm
by rooky
Hi everybody,
I have the following code and I would like to redirect the user to another page after been allow it. I've been using the header() funtion, but it can't be used after an echo tag. Can you please help me to identify a place where to locate that funtion?
This is my code:

Code: Select all

<?PHP
// Configura los datos de tu cuenta
$dbhost='db22.perfora.net';
$dbusername='dbo252002289';
$dbuserpass='18WmZnT79';
$dbname='db252002289';
 
session_start();
 
// Conectar a la base de datos
mysql_connect ($dbhost, $dbusername, $dbuserpass);
mysql_select_db($dbname) or die('Cannot select database');
 
if ($_POST['username']) {
//Comprobacion del envio del nombre de usuario y password
$username=$_POST['username'];
$password=$_POST['password'];
if ($password==NULL) {
echo "Debes digitar un password";
}else{
$query = mysql_query("SELECT user_email,user_pwd FROM users WHERE user_email = '$username' AND user_activated='1'") or die(mysql_error());
$data = mysql_fetch_array($query);
if($data['user_pwd'] != $password) {
echo "Login incorrecto";
}else{
$query = mysql_query("SELECT user_email,user_pwd FROM users WHERE user_email = '$username' AND user_activated='1'") or die(mysql_error());
$row = mysql_fetch_array($query);
$_SESSION["s_username"] = $row['username'];
echo "Has sido logueado correctamente ".$_SESSION['s_username']." y puedes acceder al index.php.";
}
}
}
?>

Re: REDIRECTING A PAGE

Posted: Mon Aug 04, 2008 3:53 pm
by jaoudestudios
Why not display the info after the redirect!

You can identify which info to display (i.e. logged in) with a flag on the redirect.

Re: REDIRECTING A PAGE

Posted: Mon Aug 04, 2008 4:42 pm
by rooky
Yeah that's what I want to do, but how can I do it because I'm new with PHP and don't know all PHP tricks. Can you help me, please?

Re: REDIRECTING A PAGE

Posted: Mon Aug 04, 2008 6:46 pm
by yacahuma
if you are new to php , the best advise I can give you is , do not put your sql code in your pages. Put all that junk in a separate file

mylib.php

Code: Select all

 
<?php
 
 
function isValidUser($username,$password)
{
  //sql stuff
 
   $user = new User();//all your user data here, you define User to whatever you want
  if ($valid)
   return array(true,$user);
  else 
    return array(false,$user);
}
?>
 
 
 
page1.php

Code: Select all

 
<?php
require_once 'mylib.php';
 
if ($_POST['username']) 
{
  list($isValid,$user) = isValidUser($_POST['username'],$_POST['password']);
  if ($isValid)
  {
    session_start();
    $_SESSION['user'] = $user;
    header("Location: http://www.example.com/xxx.php"); /* Redirect browser */
    exit;
  }
  
}
?>
 

Re: REDIRECTING A PAGE

Posted: Mon Aug 04, 2008 6:59 pm
by RobertGonzalez
Why not offer a link that says "Click here to continue" that takes the user to the redirected page? Why do you need to force redirect afterward?

Re: REDIRECTING A PAGE

Posted: Mon Aug 04, 2008 9:26 pm
by rooky
Hi guys,
Everah, is there a way to just make that "click to continue" as you recommend to appear as soon as the user has been authenticated?
Can you help me that way, please.

Re: REDIRECTING A PAGE

Posted: Mon Aug 04, 2008 10:18 pm
by rooky
Hi guys again,
I already found the solution to my problem. What I did was to insert this at the end after the user has been authenticated:

Code: Select all

echo "<BR/>";
echo "<font size='-1'>(<a href='http://www.example.com/xxxx.html'>Click here to redirect</a>)</font>";

Re: REDIRECTING A PAGE

Posted: Tue Aug 05, 2008 2:15 am
by jaoudestudios
echo "<BR/>";
Should be lower case!

Code: Select all

 
echo "<br />";
 

Re: REDIRECTING A PAGE

Posted: Tue Aug 05, 2008 11:47 am
by rooky
Oh sorry for that mistake!
See ya guys and thanks a lot!
Do I need to add any points to all of you for the help you gave me?

Re: REDIRECTING A PAGE

Posted: Tue Aug 05, 2008 12:38 pm
by jaoudestudios
I dont think so, but thanks for the thought! :D

Re: REDIRECTING A PAGE

Posted: Wed Aug 06, 2008 10:42 am
by rooky
Ok bye and thanks again!

Re: REDIRECTING A PAGE

Posted: Wed Aug 06, 2008 11:58 am
by jwalsh
My preferred solution: You could simply buffer all output in PHP to ensure that nothing has been output before the header() redirect.

Ref: http://us3.php.net/ob_start

If you want to go the route of the "click here to continue" link, you could still use javascript to force the redirect as well. That way they will only need to click if they have javascript disabled.

Code: Select all

 
window.location = "http://www.yourlocation.com/";
<a href="http://www.yourlocation.com">Click here to continue</a>