URL redirection

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
xodus
Forum Newbie
Posts: 6
Joined: Wed Mar 12, 2008 2:49 am

URL redirection

Post by xodus »

How can I redirect an user to the URL that he had typed before authentication?
User avatar
webspider
Forum Commoner
Posts: 52
Joined: Sat Oct 27, 2007 3:29 am

Re: URL redirection

Post by webspider »

Code: Select all

 
<?php
header("Location: http://example.com/"); 
?>
 
eggandzz
Forum Newbie
Posts: 1
Joined: Wed Mar 12, 2008 2:16 am

Re: URL redirection

Post by eggandzz »

use session to save the URL data at page, redirect the url after logged in
radicalfix
Forum Newbie
Posts: 7
Joined: Mon Dec 04, 2006 10:49 pm

Re: URL redirection

Post by radicalfix »

Hi Xodus,

Eggandzz and Webspider methods are correct if they are used together. But take extra cautious with the redirection, because it will not work with a any text sent to browser before PHP header() function, for example the code below:

Code: Select all

 
<html>
<?php
//this will NOT work, the browser received the HTML tag before the script
header( 'Location: http://www.example.com' ) ;
?>
 
xodus
Forum Newbie
Posts: 6
Joined: Wed Mar 12, 2008 2:49 am

Re: URL redirection

Post by xodus »

Hello all, thank you so much.
I am using a code that I have downloaded, I am unable to manipulate it to redirect it to an URL typed earlier (before authentication).
Here is the code:
$afterlogin is stored in the config file where we can change it to the desired page after login.
Please help

include_once("config.php");
include_once("lang/lang_".$lang.".php");
$pml_title = $site_name;
include("htmltop.php");
include_once("connect.php");

f(isset($_SESSION['user_id'])) {
header("Location: ".$afterlogin);
}else{
if(isset($_COOKIE['user_id'])) {
// Read cookie, make session
$sql = "SELECT id,state,password,active FROM `".$db_tbl."` WHERE id='".$_COOKIE['user_id']."'";
$query = mysql_query($sql);
$row = mysql_fetch_object($query);
$id = htmlspecialchars($row->id);
$status = htmlspecialchars($row->state);
$dbpass = htmlspecialchars($row->password);
$actief = htmlspecialchars($row->active);
if($dbpass == $_COOKIE['user_password'] AND $actief == 1) {
$_SESSION['user_id'] = $id;
$_SESSION['user_status'] = $status;
?>
<script language="Javascript" type="text/javascript">
location.href='<?= $afterlogin ?>';
</script>
<?
}else{
echo $login_cookiefalse;
setcookie("user_id", "", time() - 3600);
setcookie("user_password", "", time() - 3600);
}
}else{
if(isset($_POST['submit'])) {
// Login
$sql = "SELECT id,name,password,state,active,cookie_pass FROM `".$db_tbl."` WHERE name='".$_POST['user']."'";
$query = mysql_query($sql);
$count = mysql_num_rows($query);
if($count == 1) {
$row = mysql_fetch_object($query);
$dbpass = htmlspecialchars($row->password);
$userpass = md5($_POST['pass']);
$cookiepass = htmlspecialchars($row->cookie_pass);
$userid = htmlspecialchars($row->id);
$userstatus = htmlspecialchars($row->state);
$useractief = htmlspecialchars($row->active);
if($dbpass == $userpass) {
if($useractief == 1) {
$_SESSION['user_id'] = $userid;
$_SESSION['user_status'] = $userstatus;
if($_POST['cookie'] == "do") {
if($cookiepass == "") {
$cookiecode = mt_srand((double)microtime()*100000);
while(strlen($cookiecode) <= 10) {
$i = chr(mt_rand (0,255));
if(eregi("^[a-z0-9]$", $i)) {
$cookiecode = $cookiecode.$i;
}
}
$sql = "UPDATE `".$db_tbl."` SET cookie_pass = '".$cookiecode."' WHERE name = '".$_POST['user']."' LIMIT 1";
mysql_query($sql);
$cookiepass = $cookiecode;
}
setcookie("cookie_id", $userid, time() + 365 * 86400);
setcookie("cookie_pass", $cookiepass, time() + 365 * 86400);
}
echo $loginsucces;
?>
<script language="Javascript" type="text/javascript">
location.href='<?= $afterlogin ?>';
</script>
<?
}else{
echo $login_noactive;
}

}else{
echo $login_nopass;
}
}else{
echo $login_usererr;
}
}else{
// Loginform
?>
<form method="post" action="login.php">
<table align="center">
<tr>
<td><label for="user"><?= $login_username ?>:</label></td><td><input id="user" type="text" name="user" value="turbocamindia" /></td>
</tr>
<tr>
<td><label for="pass"><?= $login_password ?>:</label></td><td><input id="pass" type="password" name="pass" /></td>
</tr>
<tr style="visibility:hidden">
<td align="right"><input id="cookie" type="checkbox" name="cookie" value="do" style="border: 0px;" /></td><td><label for="cookie"><small><?= login_cookied ?></small></label></td>
</tr>
<tr>
<td></td><td><input type="submit" name="submit" value="<?= $login_login ?>" /></td>
</tr>
</table>
</form>


<?
}
}
}
?>
anto91
Forum Commoner
Posts: 58
Joined: Mon Mar 10, 2008 10:59 am
Location: Sweden

Re: URL redirection

Post by anto91 »

i will not give the answere untill you repost your code using php tags !
radicalfix
Forum Newbie
Posts: 7
Joined: Mon Dec 04, 2006 10:49 pm

Re: URL redirection

Post by radicalfix »

Xodus,

You can use highlight your code with the button Code.
xodus
Forum Newbie
Posts: 6
Joined: Wed Mar 12, 2008 2:49 am

Re: URL redirection

Post by xodus »

Sorry... Here it is.

Code: Select all

 
<?php
include_once("config.php");
include_once("lang/lang_".$lang.".php");
$pml_title = $site_name;
include("htmltop.php");
include_once("connect.php");
 
if(isset($_SESSION['user_id'])) {
 header("Location: ".$afterlogin);
}else{
 if(isset($_COOKIE['user_id'])) {
  // Read cookie, make session
  $sql = "SELECT id,state,password,active FROM `".$db_tbl."` WHERE id='".$_COOKIE['user_id']."'";
  $query = mysql_query($sql);
  $row = mysql_fetch_object($query);
  $id = htmlspecialchars($row->id);
  $status = htmlspecialchars($row->state);
  $dbpass = htmlspecialchars($row->password);
  $actief = htmlspecialchars($row->active);
  if($dbpass == $_COOKIE['user_password'] AND $actief == 1) {
   $_SESSION['user_id'] = $id;
   $_SESSION['user_status'] = $status;
   ?>
   <script language="Javascript" type="text/javascript">
    location.href='<?= $afterlogin ?>';
   </script>
   <?
  }else{
   echo $login_cookiefalse;
   setcookie("user_id", "", time() - 3600);
   setcookie("user_password", "", time() - 3600);
  }
 }else{
  if(isset($_POST['submit'])) {
   // Login
   $sql = "SELECT id,name,password,state,active,cookie_pass FROM `".$db_tbl."` WHERE name='".$_POST['user']."'";
   $query = mysql_query($sql);
   $count = mysql_num_rows($query);
   if($count == 1) {
    $row = mysql_fetch_object($query);
    $dbpass = htmlspecialchars($row->password);
    $userpass = md5($_POST['pass']);
    $cookiepass = htmlspecialchars($row->cookie_pass);
    $userid = htmlspecialchars($row->id);
    $userstatus = htmlspecialchars($row->state);
    $useractief = htmlspecialchars($row->active);
    if($dbpass == $userpass) {
     if($useractief == 1) {
      $_SESSION['user_id'] = $userid;
      $_SESSION['user_status'] = $userstatus;
      if($_POST['cookie'] == "do") {
       if($cookiepass == "") {
        $cookiecode = mt_srand((double)microtime()*100000);
        while(strlen($cookiecode) <= 10) {
         $i = chr(mt_rand (0,255));
         if(eregi("^[a-z0-9]$", $i)) {
         $cookiecode = $cookiecode.$i;
         }
        }
        $sql = "UPDATE `".$db_tbl."` SET cookie_pass = '".$cookiecode."' WHERE name = '".$_POST['user']."' LIMIT 1";
        mysql_query($sql);
        $cookiepass = $cookiecode;
       }
       setcookie("cookie_id", $userid, time() + 365 * 86400);
       setcookie("cookie_pass", $cookiepass, time() + 365 * 86400);
      }
      echo $loginsucces;
      ?>     
      <script language="Javascript" type="text/javascript">
       location.href='<?= $afterlogin ?>';
      </script>
      <?
     }else{
      echo $login_noactive;
     }  
 
   }else{
     echo $login_nopass;
    }
   }else{
    echo $login_usererr;
  }
  }else{
   // Loginform
   ?>
   <form method="post" action="login.php">
    <table align="center">
     <tr>
      <td><label for="user"><?= $login_username ?>:</label></td><td><input id="user" type="text" name="user" value="turbocamindia" /></td>
     </tr>
     <tr>
      <td><label for="pass"><?= $login_password ?>:</label></td><td><input id="pass" type="password" name="pass" /></td>
     </tr>
     <tr style="visibility:hidden">
      <td align="right"><input id="cookie" type="checkbox" name="cookie" value="do" style="border: 0px;" /></td><td><label for="cookie"><small><?= login_cookied ?></small></label></td>
     </tr>
    <tr>
      <td></td><td><input type="submit" name="submit" value="<?= $login_login ?>" /></td>
     </tr>
     </table>
   </form>
   
   
   <?
   }
   }
   }
    ?>
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Re: URL redirection

Post by miro_igov »

You should post the content of the include files too. if include("htmltop.php"); includes something that outputs html header() redirects may not work.
xodus
Forum Newbie
Posts: 6
Joined: Wed Mar 12, 2008 2:49 am

Re: URL redirection

Post by xodus »

Config.php

Code: Select all

 
<?php
 
session_start();
ob_start();
 
// MySQL settings
$db_user    = "root"; // MySQL username
$db_pass    = "turbocam"; // MySQL password
$db_host    = "localhost"; // MySQL host, mostly localhost
$db_db      = "tcam"; // MySQL database
$db_tbl     = "phpmylogon"; // MySQL table
 
// Settings
$site_url   = ""; // URL to PhpMyLogon, / at end
$site_mail  = ""; // Mail address website
$site_name  = ""; // Website name
$activate   = ""; // E-mail validation at registration, TRUE or FALSE
$afterlogin = "index.php"; // Page to go to after login
$lang       = "en"; // Language of PhpMyLogon
?>
 
htmltop.php

Code: Select all

 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title><?= $pml_title ?></title>
<style type="text/css">
<!--
.text1 {
    font-family:"Trebuchet MS";
    font-size:8.5pt;
    line-height:13px; 
    color:#CCCCCC;
    text-decoration:none;
        }
-->
</style>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" href="style.css" />
<script language="JavaScript" type="text/javascript">
<!--
function confirmLink(link, text) {
 if(typeof(window.opera) != 'undefined') {
  return true;
 }
 var is_confirmed = confirm(text);
 if (is_confirmed) {
  location.href = link;
 }
 return is_confirmed;
}
 
function addtext(veld,text) {
 text = ''+text+' ';
 if(document.form.elements[veld].createTextRange) {
  document.form.elements[veld].focus();
  document.selection.createRange().duplicate().text = text;
 }else{
  document.form.elements[veld].focus();
  document.form.elements[veld].value += text;
 }
}
 
//-->
</script>
<script src="../Scripts/AC_RunActiveContent.js" type="text/javascript"></script>
</head>
<body>
 
<table width="775" border="0" cellpadding="0" cellspacing="0" align="center">
  <!--DWLayoutTable-->
  <tr>
    <td width="775" height="155" align="center" valign="top">
    
  <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="775" height="350">
    <param name="movie" value="intro.swf" />
    <param name="quality" value="high" />
    <embed src="intro.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="775" height="350"></embed>
  </object>
    <br /></td>
  </tr>
</table>
 
Post Reply