REDIRECTING A PAGE

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rooky
Forum Newbie
Posts: 15
Joined: Thu Jul 31, 2008 10:03 pm

REDIRECTING A PAGE

Post 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.";
}
}
}
?>
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: REDIRECTING A PAGE

Post 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.
rooky
Forum Newbie
Posts: 15
Joined: Thu Jul 31, 2008 10:03 pm

Re: REDIRECTING A PAGE

Post 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?
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: REDIRECTING A PAGE

Post 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;
  }
  
}
?>
 
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: REDIRECTING A PAGE

Post 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?
rooky
Forum Newbie
Posts: 15
Joined: Thu Jul 31, 2008 10:03 pm

Re: REDIRECTING A PAGE

Post 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.
rooky
Forum Newbie
Posts: 15
Joined: Thu Jul 31, 2008 10:03 pm

Re: REDIRECTING A PAGE

Post 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>";
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: REDIRECTING A PAGE

Post by jaoudestudios »

echo "<BR/>";
Should be lower case!

Code: Select all

 
echo "<br />";
 
rooky
Forum Newbie
Posts: 15
Joined: Thu Jul 31, 2008 10:03 pm

Re: REDIRECTING A PAGE

Post 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?
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: REDIRECTING A PAGE

Post by jaoudestudios »

I dont think so, but thanks for the thought! :D
rooky
Forum Newbie
Posts: 15
Joined: Thu Jul 31, 2008 10:03 pm

Re: REDIRECTING A PAGE

Post by rooky »

Ok bye and thanks again!
User avatar
jwalsh
Forum Contributor
Posts: 202
Joined: Sat Jan 03, 2004 4:55 pm
Location: Cleveland, OH

Re: REDIRECTING A PAGE

Post 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>
 
Post Reply