Im trying to create a php to redirect multiple urls with an specific ID for example
mysite.com/url.php?id=1
id=1 will automatically redirect to www.google.com
mysite.com/url.php?id=2
id = 2 will automatically redirect to www.bing.com
successively ...
I think that an array can simplify it
i will apreciate your answers.
Thanks
If you want to use a static array, then use the following code:
<?php
$sites = array(
1 => 'http://domain.tld';
2 => 'http://anotherdomain.tld';
);
if (isset($_GET['id']) && array_key_exists($_GET['id']), $sites) {
header('location: ' . $sites[$_GET['id']]);
} else {
// 404?
}
?>
You can use something like this:
<?php
$id = isset($_GET['id']) ? $_GET['id'] : '';
$id = (int)(trim($id));
switch($id){
case 1:
header("Location: http://www.google.com/");exit;
break;
case 2:
header("Location: http://www.bing.com/");exit;
break;
default:
header("Location: http://www.default.com/");exit;
break;
}
?>
Related
<?php
if(isset($_POST['MarkaSinifi'])) {
$MarkaSinifi = $_POST['MarkaSinifi'];
echo "https://www.xxxxx.com/cart.php?a=add&pid=3&configoption[1]=2&customfield[10]=webx";
foreach($MarkaSinifi as $marka) {
echo '&configoption[' . $marka . ']=1';
}
}
?>
I made that code for redirect user every $marka has a value like 1,2,3 this is the result for the link i want to redirect: "https://www.xxxxx.com/cart.php?a=add&pid=3&configoption[1]=2&customfield[10]=webx&configoption[51]=1&configoption[52]=1"
This is just text, but i want to redirect users to that link. How can i do that?
This is what i did: https://coinearn.shop/marka.php
If you know all index of $marka, you can use
<?php
header('Location: '. "https://www.xxxxx.com/cart.php?a=add&pid=3&configoption$marka[0]=2&customfield[10]=webx&configoption$marka[1]=1&configoption$marka[2]=1");
This can help too
<?php
$args[0] = 'Location: https://www.xxxxx.com/cart.php?a=add&pid=3&configoption%s=2&customfield[10]=webx&configoption%s=1&configoption%s=1';
$args = array_merge($args, $marka);
header(call_user_func_array('sprintf', $args);
Hey guys I am trying to build a redirect script in my header.
It contains a variable called $redirect that either equals 0 or 1.
What I want to do is if the variable equals 1 to redirect the user to a specified page.
That works.
The problem I am having is when it reaches the redirected URL it creates a loop.
I tried writing the following code but it does not work. What have I done wrong?
<?php
$redirect = 1;
$host = $_SERVER['HTTP_HOST'];
$self = $_SERVER['PHP_SELF'];
$query = !empty($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : null;
$url = !empty($query) ? "http://$host$self?$query" : "http://$host$self";
$redirectURL = '/protest/cispa.php';
if ( $redirect === 1 ) {
if ( $url === $redirectURL ) {
die();
}
else {
header("Location: $redirectURL");
exit;
}}
?>
As suggested by andrewsi I updated my code to the following at it works:
<?php
$redirect = 0;
$host = $_SERVER['HTTP_HOST'];
$self = $_SERVER['PHP_SELF'];
$query = !empty($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : null;
$url = $self;
$redirectURL = '/protest/cispa.php';
if ( $redirect === 1 ) {
if ( $url === $redirectURL ) {
}
else {
header("Location: $redirectURL");
exit;
}
}
?>
$url = !empty($query) ? "http://$host$self?$query" : "http://$host$self";
$redirectURL = '/protest/cispa.php';
Your $url contains a fully qualified domain name; the redirectURL is just a path. The two are never going to be equal. Try setting :
$url = $self;
(If I'm reading your code correctly)
You could stick a flag in the query string of your redirect. Then, if the flag is present, don't add the redirect.
ex;
http://www.yoursite.com/redirectedto.php?red=1
Now, if red is set, I would not add the redirect.
lee
Basically I'm grabbing a variable from the url and I need to send the user to custom pages based on what state they fill in. i.e. if it's one of the coverred states, they'll get sent to custom pages...but if it's any other state they get sent to the standard one.
I'm guessing it's just a simple if statement with an else...but for some reason I can't get it to work properly.
<?php
$state = urlencode($_GET['state']);
if ($state=="california"){$link = "http://website.com/page1";}
else if ($state=="new york"){$link = "http://website.com/page2";}
else if ($state=="ohio"){$link = "http://website.com/page3";}
else {$link = "http://website.com/default";}
header("Location: $link");
?>
Is this right or should I be doing something else?
You don't need urlencode function to encode what you "GET", unless you sent the string in the url has been encoded. However, in that case you would need the urldecode function, still not urlencode.
So your code should be like this:
<?php
$state = $_GET['state'];
//or $state = urldecode($_GET['state']);
//if you are receving an encoded url.
if ($state=="california"){$link = "http://website.com/page1";}
else if ($state=="new york"){$link = "http://website.com/page2";}
else if ($state=="ohio"){$link = "http://website.com/page3";}
else {$link = "http://website.com/default";}
header("Location: $link");
?>
Also, check the 'state' in the url. Did you receive the correct string that you need? Try to echo $state, and see what you get.
The urlencode is not needed and even introduces a bug (you will never match strings such as "new york", because urlencode will turn $state into "new+york").
Other than that the code looks OK. Just remove it and you 'll be good.
No need to use urlencode
<?php
$state = $_GET['state'];
if ($state=="california"){
$link = "http://website.com/page1";
} else if ($state=="new york") {
$link = "http://website.com/page2";
} else if ($state=="ohio"){
$link = "http://website.com/page3";
} else {
$link = "http://website.com/default";}
header("Location: $link");
?>
It is tidier to do this with switch, and combine the repeated stuff:
switch($_GET['state']) {
case "california": $page = "page1"; break;
case "new york": $page = "page2"; break;
case "ohio": $page = "page3"; break;
default: $page = "default"; break;
}
header("Location: http://website.com/".$page);
Another option is to use an associative array; this is attractive if there are lots of choices.
$statemap = array('california' => 'page1',
'new york' => 'page2',
'ohio' => 'page3');
$state = $_GET['state'];
$link = 'http://website.com/' . array_key_exists($state, $statemap) ? $statemap[$state] : 'default'
header("Location: $link");
I am writing what I thought would be a simple script but I am stuck.
The scenario is that I want to create 2 strings from the GET request.
eg: domain.com/script.php?Client=A12345
In script.php it needs to grab the "Client" and create 2 variables. One is $brand and needs to grab the A or B from the URL. The Other is $id which needs to grab the 12345 from the URL.
Now, after it has these 2 variables $brand and $id it needs to have an if statement to redirect based on the brand like below
if ($brand=="A") {
header('Location: http://a.com');
}
if ($brand=="B") {
header('Location: http://b.com');
At the end of each URL I want to apend the $id though and I am unsure on how to do this.
So for example I would access the script at domain.com/script?Client=A1234 and it needs to redirect me to a.com/12345
Thanks in advance!
$fullCode = $_REQUEST['Client'];
if(strpos($fullCode, 'A') !== false) {
$exp = explode('A',$fullcode);
header('Location: http://a.com/' . $exp[1]);
}
else if(strpos($fullCode, 'B') !== false) {
$exp = explode('B',$fullcode);
header('Location: http://b.com/' . $exp[1]);
}
else {
die('No letter occurence');
}
You can easily do,
$value = $_GET['Client'];
$brand = substr($value, 0, 1);
$rest = substr($value, 1, strlen($brand)-1);
now you have the first character in $brand string and you can do the if statement and redirect the way you want...
You mean like this?
Notice: this will only work if brand is just 1 character long. If that's not the case, please give better examples.
<?php
$client = $_GET['Client'];
$brand = strtolower(substr($client, 0, 1));
$id = substr($client, 1);
if ($brand == 'a')
{
header("Location: http://a.com/$id");
}
elseif ($brand == 'b')
{
header("Location: http://b.com/$id");
}
?>
Try using:
preg_match("/([A-Z])(\d*)/",$_GET['Client'],$matches);
$matches[1] will contain the letter and $matches[2] will contain your id.
Then you can use:
if ($matches[1]=="A")
{
header('Location: http://a.com/{$matches[2]}');
}
if ($matches[1]=="B")
{
header('Location: http://b.com/{$matches[2]}');
}
suggest you could also try
$requested = $_GET["Client"];
$domain = trim(preg_replace('/[^a-zA-Z]/',' ', $requested)); // replace non-alphabets with space
$brand = trim(preg_replace('/[a-zA-Z]/',' ', $requested)); // replace non-numerics with space
$redirect_url = 'http://' . $domain . '/' . $brand;
header('Location:' . $redirect_url);
but it'd be better if you could get the domain name and brand as two individual parameters and sanitize them individually before redirecting them to prevent the overhead of extracting them from a single parameter.
Note: this expression might be useless when the domain name itself has numerics and because the Client is obtained through get a good deal of validation and sanitation would be required in reality.
$brand = strtolower($_GET['Client'][0]);
$id = substr($_GET['Client'], 1);
header("Location: http://{$brand}.com/{$id}");
If for some purpose you want to use explode, then you need to have a separator.
Let's take '_' as the separator, so your example would be something like this: domain.com/script.php?Client=A_12345
$yourstring = explode("_",$_GET["Client"]);
echo $yourstring[0];
//will output A
echo $yourstring[1];
//will output 12345
//your simple controller could be something like this
switch($yourstring[0]){
case: 'A':
header('Location: http://a.com?id='.$yourstring[1]);
exit();
break;
case: 'B':
header('Location: http://b.com?id='.$yourstring[1]);
exit();
break;
default:
//etc
}
I'm trying to use the break concept, but I am also am using some built in PHP functions to only allow certain values into the get function.
$allowedKeys = array(
'route'
);
$options = array(
'chapter_1' => 'chapter_1',
'chapter_2' => 'chapter_2',
'chapter_3' => 'chapter_3'
);
$_GET = array_intersect_key($_GET, array_flip($allowedKeys));
if($_GET[$allowedKeys[0]] && array_key_exists($_GET[$allowedKeys[0]], $options)) {
if($_GET[$allowedKeys[0]] == $options[0]) {
/* This is where I'm trying to see if route=chapter_1 then do something.
The logic I'm trying to write is if the route is chapter_1 then print
out the content from chapter 1 How can determine this? */
echo "Hello";
}
}
Why isn't this code echoing "hello"?
Why make it more complex than it needs to be?
//check you have a route
$route = isset( $_GET['route'] ) ? $_GET['route'] : '';
switch( $route ) {
case 'chapter_1':
//do chapter one stuff
echo 'Chapter 1';
break;
case 'chapter_2':
//do chapter two stuff
echo 'Chapter 2';
break;
default:
echo 'Intro';
}
I'll answer your question directly for you. 'Hello' isn't showing because it is inside an if statement that isn't being triggered because either "if($_GET[$allowedKeys[0]] && array_key_exists($_GET[$allowedKeys[0]], $options))" or "if($_GET[$allowedKeys[0]] == $options[0])" is returning false.