Want to continuous redirect 302 - php

Now i want to continuous redirect my site.
Example: Visitor go to: MyDomain/abc?id=1
They will be redirect 302 to: MyDomain/RandomString1
and continue Redirect to MyDomain/RandomString2
and continue:.... until to MyDomain/RandomString10
with Random string is random string and:
RandomString1 to 10 is not exist.
Please help me. Bellow attachment is demo
Here is demo image

Not exactly sure what your looking for but as i understand the question this is what i have to offer
<?
$numberofstrings = 1;
$i = $numberofstrings;
$string = "url";
while($i < $numberofstrings){
$i++;
$currentString = $string . "?id=" . $i;
header("Location: $currentString")
}
$id = $_GET['id'];
if($_GET['id'] == 1){
header("Location: url");
} elseif($id == 2){
header("Location: url2");
}
?>

Related

Two redirects after each other in PHP

I have a form on frontend page that after submiting should redirect to a page that API returns but I must redirect first to a thank you page and then after some seconds should redirect to the page that the API returns.
this is so far what I have tried but it only waits for the second refresh.
if(isset($result_array['status']) && $result_array['status'] == true)
{
$autologin_url = $result_array['data'] . PHP_EOL;
$signup_result = true;
header("refresh: 5; url = fakeurl.com/thanks.php");
header("refresh: 15; url = $autologin_url");
}
else
{
echo $result;
$signup_result = false;
header('location: fakeurl.com/?report=signup_error');
}
This is what brombeer meant by the redirect:
if(isset($result_array['status']) && $result_array['status'] == true)
{
$autologin_url = $result_array['data'] . PHP_EOL;
$signup_result = true;
header("refresh: 5; url = fakeurl.com/thanks.php");
}
else
{
echo $result;
$signup_result = false;
header('location: fakeurl.com/?report=signup_error');
}
And Paste this in your thanks.php Script:
header("refresh: 15; url = $autologin_url");

dynamically name session isset how to use?

I am trying to get dynamic $_SESSION[$id] on the second page shown below, but its not working (as per the printout):
First page url
https://example.com/test.php?id=1548393
First page code
<?php
session_start();
$id = $_GET['id'];
$_SESSION[$id] = "mysecretstringline";
?>
Second page url
https://example.com/test2.php?id=1548393
Second page code
<?php
session_start();
$id = $_GET['id'];
if(isset($_SESSION[$id])){
echo "working";
}else{
echo "not working";
}
?>
i found problem we can not use numeric index for $_SESSION
but we can use number in $_SESSION by convert number to roman numerals
first page url
https://example.com/test.php?id=1548393
first page code
<?php
session_start();
$roman_id = romanic_number($_GET['id']);
$_SESSION[$roman_id] = "mysecretstringline";
function romanic_number($integer, $upcase = true)
{
$table = array('M'=>1000, 'CM'=>900, 'D'=>500, 'CD'=>400, 'C'=>100, 'XC'=>90, 'L'=>50, 'XL'=>40, 'X'=>10, 'IX'=>9, 'V'=>5, 'IV'=>4, 'I'=>1);
$return = '';
while($integer > 0)
{
foreach($table as $rom=>$arb)
{
if($integer >= $arb)
{
$integer -= $arb;
$return .= $rom;
break;
}
}
}
return $return;
}
?>
second page url
https://example.com/test2.php?id=1548393
second page code
<?php
session_start();
$roman_id = romanic_number($_GET['id']);
if(isset($_SESSION[$roman_id])){
echo "working";
}else{
echo "not working";
}
function romanic_number($integer, $upcase = true)
{
$table = array('M'=>1000, 'CM'=>900, 'D'=>500, 'CD'=>400, 'C'=>100, 'XC'=>90, 'L'=>50, 'XL'=>40, 'X'=>10, 'IX'=>9, 'V'=>5, 'IV'=>4, 'I'=>1);
$return = '';
while($integer > 0)
{
foreach($table as $rom=>$arb)
{
if($integer >= $arb)
{
$integer -= $arb;
$return .= $rom;
break;
}
}
}
return $return;
}
?>
output
working
thanks #gre_gor and #Katie
Could be that in your normal code (this just looks like a quick mockup), you have a space after ?> somewhere. That could cause issues.
<?php
// start.php
session_start();
$id = $_GET['id'];
$_SESSION[$id] = "mysecretstringline";
and
<?php
// next.php
session_start();
$id = $_GET['id'];
if (isset($_SESSION[$id])) {
echo "working";
} else {
echo "not working";
}
works for me. Notice no ?> characters.
UPDATE:
The following might be of interest regarding session name constraints (can a php $_SESSION variable have numeric id thus : $_SESSION['1234’])
You have that issue in your example you could just add an id_ and then do the same check when validating/getting the session.

Redirect to page only once?

I have this piece of php code that looks up the account prompt function on my website. basically if a user has violated a term and condition on the site, at login they are redirected to a prompt page that says you are very naughty and here's a warning.
My code is this:
<?php
$account_prompt = account_prompt();
while ($prompt = mysql_fetch_array($account_prompt))
if ($prompt['account_prompt'] == '1') {
redirect_to("prompt.php");
}
?>
My question is how can i get it to only redirect once?
Thanks
He just redirects 1ce unless you are stucked in an endless loop...
Try this
if (isset($prompt['account_prompt']) && $prompt['account_prompt'] == '1') {
header("Location: prompt.php");
exit;
}
Use a flag which is set to 1 according to your condition, and take the redirect out of the loop.
$flag = 0;
while ($prompt = mysql_fetch_array($account_prompt)) {
if ($prompt['account_prompt'] == '1') $flag = 1;
}
if ($flag == 1)
redirect_to("prompt.php");

PHP GET String with explode and if statements

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
}

Restriction based on IP ranges

I am building an admin panel. and I want to block certain IP ranges. I'm testing this on my localhost wamp server but ir doesn't seem to redirect me.
<?php
if($_SERVER['REMOTE_ADDR'] == '127.0.0..*')
header("Location: http://google.com");
else
echo "Hai";
?>
Any input is appreciated.
Is sufficient to use string comparison
if (strncmp('127.0.0.', $_SERVER['REMOTE_ADDR'], 8) === 0)
header("Location: http://google.com");
else
echo "Hai";
Update: Taken from the comments of inits answer
Suppose i want to block any IP coming from this range: 192.168.0.1-255. What would be the best solution for it ? Thanks.
Then just make the same string comparison against this block
if (strncmp('192.168.0.', $_SERVER['REMOTE_ADDR'], 10) === 0)
header("Location: http://google.com");
else
echo "Hai";
If you want to test the remote address against both blocks at once, you will probably put them together into one expression. This time we need a different approach
if (in_array(substr($_SERVER['REMOTE_ADDR'], 0, strrpos($_SERVER['REMOTE_ADDR'], '.')), array('127.0.0', '192.168.0'))) {
header("Location: http://google.com");
else
echo "Hai";
The substr()-part takes the IP until the last .. We can just try to find this string in a set (-> array) of IP-prefixes.
$ip0 = ip2long("127.0.0.1");
$ip1 = ip2long("127.0.0.254");
$ip = ip2long($_SERVER['REMOTE_ADDR']);
if ($ip0 <= $ip && $ip <= $ip1) {
echo long2ip($ip) . " is inside range " . long2ip($ip0) . "-" . long2ip($ip1);
}
else {
echo long2ip($ip) . " is outside range " . long2ip($ip0) . "-" . long2ip($ip1);
}
This would be a better approach, using regualr expression:
// returns true for IPs 127.0.0.0-255
if (preg_match("'^127[.]0[.]0[.][0-9]+'",$_SERVER['REMOTE_ADDR']))
{
header("Location: http://google.com");
}
else
{
echo "Hai";
}
EDIT: Fine, take it to another level, maybe not the most effective, but easier to configure:
$mask = "192.168.1.1-255";
$ip = explode(".",$_SERVER['REMOTE_ADDR']);
$in = 0;
foreach (explode(".",$mask) as $k => $v)
{
if (preg_match("'^([0-9]+)-([0-9]+)$'",$v,$n))
{
if ($ip[$k] >= $n[1] && $ip[$k] <= $n[2]) $in++;
}
elseif (preg_match("'^[0-9]+$'",$v,$n))
{
if ($ip[$k] == $n[0]) $in++;
}
}
if ($in == 4)
{
header("Location: http://google.com");
}
else
{
echo "Hai";
}
Here is my solution to the problem of just allowing some fixed IPs and some IP ranges:
$ClientIP = $_SERVER['REMOTE_ADDR'];
$First3PartsOfIP = substr($ClientIP, 0, strrpos($ClientIP, '.'));
$AllowedIPs = ['127.0.0.1'];
$AllowedFirst3Parts = ['172.20.8', '172.21.13'];
if (!in_array($ClientIP, $AllowedIPs) && !in_array($First3PartsOfIP, $AllowedFirst3Parts)) {
//echo "Your IP: $ClientIP<br />";
die("Access Denied!");
}

Categories