sample code required for redirecting to another 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
crazytopu
Forum Contributor
Posts: 259
Joined: Fri Nov 07, 2003 12:43 pm
Location: London, UK
Contact:

sample code required for redirecting to another page

Post by crazytopu »

hi guys,

this is what i want:

once the library administrator hits the "log in" button i want to redirect him/her a page where from they can do some administrative work - i.e. insert book (insertbook.php) , delete book (deletebook.php);

so what i m so far able to do is show them a welcome messege once they logged in as administrator.

here is the code...please take a look at - Here i have used comment to tell my requirement more clearly...it is near the welcome messege...

Code: Select all

<?php
 
<html> 
<head> 
<title>Login</title> 
</head> 
<body> 
        <?php 
/* database connect script. */ 
require 'db_connect.php'; 
if($logged_in == 1) 
{ 
die('You are already logged in,<b> '.$_SESSION['username'].'</b>. Go back <a href = "template2.php">HOME</a>.'); 
          
} 
if (isset($_POST['submit'])) 
{ 
// if form has been submitted 
/* check they filled in what they were supposed to and authenticate */ 
if(!$_POST['uname'] | !$_POST['passwd']) 
{ 
die('You did not fill in a required field.'); 
} 
// authenticate. 
if (!get_magic_quotes_gpc()) 
{ 
$_POST['uname'] = addslashes($_POST['uname']); 
} 
$check = "SELECT username, password FROM users WHERE username = '".$_POST['uname']."'"; 
$result = mysql_query($check); 
$num_rows = mysql_num_rows($result); 
if (!($num_rows))          
{ 
die('That username does not exist in our database.'); 
} 
$info = mysql_fetch_Array($result); 
// check passwords match 
$_POST['passwd'] = stripslashes($_POST['passwd']); 
$info['password'] = stripslashes($info['password']); 
$_POST['passwd'] = md5($_POST['passwd']); 
if ($_POST['passwd'] != $info['password']) 
{ 
die('Incorrect password, please try again.'); 
} 
/* if we get here username and password are correct, 
register session variables and set last login time.*/ 
$date = date('m d, Y'); 
$update_login = mysql_query("UPDATE users SET last_login = '$date' WHERE username = '".$_POST['uname']."'"); 
$_POST['uname'] = stripslashes($_POST['uname']); 
$_SESSION['username'] = $_POST['uname']; 
$_SESSION['password'] = $_POST['passwd']; 
// Remember Me cookie will be set after successful login 
if (isset($_POST['remember_me'])) { 
$time_expire = time()+31536000; 
setcookie("uname", $_POST['uname'], $time_expire); 
setcookie("passwd", md5($_POST['passwd']), $time_expire); 
}

?>
        <table border="0" width="100%">
          <tr>
            <td width="100%"><b>Logged in</b></td>
          </tr>
        </table>
        <table border="0" width="100%">
          <tr>
            <td width="100%">
              <form action="<?php echo $_SERVER['PHP_SELF']?>?var=logout" method="post"> 

                <p><input type="submit" value="logout" name="logout"></p>
              </form>
              <p>&nbsp;</td>
          </tr>
        </table>




welcome //the welcome messege.....

//here i would like to redirect the user to a new page...how to do that? lets say i want
to redirect them to insert.php page. 







<?php

} 
else 
{ 
// if form hasn't been submitted 
?> 
        <!-- This is the first screen when a user sees when he is not logged in --> 
        <form action="<?php echo $_SERVER['PHP_SELF']?>?var=login" method="post"> 
        <center> 
        <table width="250" border="1" cellspacing="0" cellpadding="4" bordercolor="#000000" bordercolordark="#000000" bordercolorlight="#000000" bgcolor="#FFFFFF" style="border-collapse: collapse" height="158"> 
        <tr> 
        <td class="updatecontent" height="75">
          <table border="0" width="100%">
            <tr>
              <td width="50%"><b>username</b></td>
              <td width="50%"><input type="text" name="uname" maxlength="40"></td>
            </tr>
            <tr>
              <td width="50%"><b>password</b></td>
              <td width="50%"> 
        <input type="password" name="passwd" maxlength="50"> 
              </td>
            </tr>
          </table>
        </td></tr> 
        <tr><td class="updatefooter" height="63"> 
        <input type="Checkbox" name="Remember_Me">  <b>Remember Me </b><font color="#000000">[applicable
        for private pc only]</font><br>                      
<input type="submit" name="submit" value="Login"> 
        </td></tr> 
        </table> 
        </form> 
<?php 
} 
?></center>
        <p>&nbsp; 



</body> 



</html> 



?>
thanks
User avatar
Pointybeard
Forum Commoner
Posts: 71
Joined: Wed Sep 03, 2003 7:23 pm
Location: Brisbane, AUS
Contact:

Post by Pointybeard »

well, to redirect you use the header function. IE

Code: Select all

header("location: URL")
but you can only do that if the page headers havent been sent, so placing the header call where your talking about would not work. Also, even if it did, your not going to see the welcome note since it will be redirected. If that doesnt matter, then just place header("location: URL"); as the first line after your <?php. There cannot be any white space at the top of the page either (before the <?php tag i mean) Hope that helps.

-PB
crazytopu
Forum Contributor
Posts: 259
Joined: Fri Nov 07, 2003 12:43 pm
Location: London, UK
Contact:

Post by crazytopu »

which <?php? in line 7?

like this?:

Code: Select all

<?php
<html> 
<head> 
<title>Login</title> 
</head> 
<body> 
<?php 
header("location: http://www.php.net/")


?>
but it does give me an error msg...what to do?
darknecromancer
Forum Newbie
Posts: 2
Joined: Tue Feb 17, 2004 12:59 am

Post by darknecromancer »

well i dont know how secure this is but you can echo to html code "location = 'WHATEVER.com'" and it should redirect them.. but i dont know if thats what you wont... just thought i would add my 2 cent, also you can do it in java script. :P
crazytopu
Forum Contributor
Posts: 259
Joined: Fri Nov 07, 2003 12:43 pm
Location: London, UK
Contact:

Post by crazytopu »

okay i have done it...


here is the part of the code:

Code: Select all

<?php
?>
        <table border="0" width="100%">
          <tr>
            <td width="100%"><b>Logged in</b></td>
          </tr>
        </table>
        <table border="0" width="100%">
          <tr>
            <td width="100%">
              <form action="<?php echo $_SERVER['PHP_SELF']?>?var=logout" method="post"> 

                <p><input type="submit" value="logout" name="logout"></p>
              </form>
              <p>&nbsp;</td>
          </tr>
        </table>


<p>Welcome <?php echo $_SESSION['username']; ?>, you are now being taken to the administrative page............</p>
<META HTTP-EQUIV="refresh" content="0;URL=http://www.php.net">
<?php

} 
else 
{ 
// if form hasn't been submitted 
?> 

?>
Thanks guys...but i m still curious to know how to do the same thing using header?
User avatar
Dr Evil
Forum Contributor
Posts: 184
Joined: Wed Jan 14, 2004 9:56 am
Location: Switzerland

Post by Dr Evil »

You were getting an error because the header("location: http://www.php.net/") can only be used if nothing is sent to the client before (no characters, no spaces and no html).

So you cannot send a message to the user and then redirect him. For this you must use (as you did) a META refresh or a Javascript redirect.

You'll have to rearange your conditions for ex:

Code: Select all

<?php
if ($redirect=='yes'){
header("location: http://www.php.net/")}

else{
?>
<html> 
<head> 
<title>Login</title> 
</head> 
<body> 
...
<?php
}
?>
Post Reply