Jump to content

Redirect to url


frank_solo

Recommended Posts

Alright so I'm trying to create a redirect script where it determines where to direct the user based on what the column equals to. Can anyone please "direct" me in how to accomplish this? This what I have so far:

<?
include('config.php');
include('lock.php');
include('timeout.php');
$db_name=""; // Database name 
$tbl_name=""; // Table name

// Connect to server and select database.

mysql_select_db("$db_name")or die("cannot select DB");

// get value of id that sent from address bar
$id=$_GET['id'];


// Retrieve data from database 
$sql="SELECT * FROM $tbl_name WHERE id='$id'";
$result=mysql_query($sql);

$rows=mysql_fetch_array($result);

$a = "apteditform.php?id=".$rows['id']."";
$b = "commeditform.php?id=".$rows['id']."";


$unit = "".$rows['unit']."";

if ( "$unit == 'Apartment'" )
  {
  header ("Location: $a");
  }
  elseif ( "$unit == 'Commercial'" )
  {
  header ("Location: $b");
  }

?>

Thanks

Link to comment
https://forums.phpfreaks.com/topic/277666-redirect-to-url/
Share on other sites

What is the problem? Tell us what it is doing that is should NOT do, or what it is NOT doing that it should.

 

Turn on error reporting (at the beginning of the script) so you can see if there are any error messages

error_reporting(E_ALL);
ini_set('display.errors', 1);
Don't use short tags "<?" use full tags instead "<?php" -- some servers do not support short tags.

 

Make sure there is absolutely no output before the header() calls (like maybe from the included files).

 

Technically, a Location header is supposed to have a full url. i.e. http://www.domain.com/apteditform.php?id=1.

Link to comment
https://forums.phpfreaks.com/topic/277666-redirect-to-url/#findComment-1428409
Share on other sites

What I would like it to do is if the column equals to "Apartments" then to direct the user to apteditform.php or if the Column equals to "Commercial" then direct the user to commeditform.php

I added error reporting and this is what it reads:

"Notice: A session had already been started - ignoring session_start() in /home/content/84/10858584/html/user/timeout.php on line 2

Warning: Cannot modify header information - headers already sent by (output started at /home/content/84/10857784/html/user/timeout.php:2) in /home/content/84/10858584/html/user/timeout.php on line 9

Warning: Cannot modify header information - headers already sent by (output started at /home/content/84/10857784/html/user/timeout.php:2) in /home/content/84/10858584/html/user/redirectedit.php on line 33"

Link to comment
https://forums.phpfreaks.com/topic/277666-redirect-to-url/#findComment-1428413
Share on other sites

Make sure there is absolutely no output before the header() calls (like maybe from the included files).

 

Technically, a Location header is supposed to have a full url. i.e. http://www.domain.com/apteditform.php?id=1.

We also have a sticky thread with how to handle these errors.

Link to comment
https://forums.phpfreaks.com/topic/277666-redirect-to-url/#findComment-1428419
Share on other sites

Ok Thanks guys but I have done both changes and it still selects $a = "http://user.mydomain.co/apteditform.php?id=".$rows['id']."";

These are the changes

<?php
error_reporting(E_ALL);
ini_set('display.errors', 1);
include('config.php');


$db_name=""; // Database name 
$tbl_name=""; // Table name

// Connect to server and select database.

mysql_select_db("$db_name")or die("cannot select DB");

// get value of id that sent from address bar
$id=$_GET['id'];


// Retrieve data from database 
$sql="SELECT * FROM $tbl_name WHERE id='$id'";
$result=mysql_query($sql);

$rows=mysql_fetch_array($result);

$a = "http://user.mydomain.co/apteditform.php?id=".$rows['id']."";
$b = "http://user.mydomain.co/commeditform.php?id=".$rows['id']."";


$unit = "".$rows['unit']."";

if ( "$unit == 'Apartment'" )
  {
  header ("Location: $a");
  }
  elseif ( "$unit == 'Commercial'" )
  {
  header ("Location: $b");
  }

?>
Link to comment
https://forums.phpfreaks.com/topic/277666-redirect-to-url/#findComment-1428422
Share on other sites

 

if ( "$unit == 'Apartment'" )
  {
  header ("Location: $a");
  }
  elseif ( "$unit == 'Commercial'" )

 

What the heck is that? Why do you have the condition you want to test in quotes? That IF statement is checking to see if the string is not empty. It is not empty because it contains "$unit == 'Apartment'" (literally).

 

if ( $unit == 'Apartment' )
  {
  header ("Location: $a");
  }
  elseif ( $unit == 'Commercial' )
Link to comment
https://forums.phpfreaks.com/topic/277666-redirect-to-url/#findComment-1428517
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.