Form submit response 301 or 302 - php

I have a form on my site and every time I submit this form..I get a 301 perm, then a 302 redirect, then another 302 redirect. My data is not submitting because of these redirects. I'm having no luck finding these redirects. There is no .htaccess file that is causing this in the root of the directory.
Where do I look to get rid of the redirects? Any tips would be appreciative.
My developer tools on submit goes to 301, 302, 302.
Here is the start of my form.
<form action="index.php?view=ticket_submit" method="POST" name="QContact" runat="vdaemon">
When this submits I have a case statement look for ticket_submit. It's not even getting to ticket_submit because of the var_dump and exit methods. It just goes back to the root index.php file.
private function ProcessView($type){
$ticket = new Ticket();
$ticket->CurrentUser = $this->CurrentUser;
$ticket->TicketUser = $this->CurrentUser;
$ticket->setBlankTicketHTML();
$title = 'New ROI';
$prior_count = count($this->Containers);
$_SESSION['vdaemon'] = '1';
if($_SESSION['al'] == 't' && $type['view'] == ''){
$type['view'] = 'summary';
}
switch($type['view']){
case 'ticket_edit':
$ticket->ticketid = (int)$this->Request['ticketid'];
$ticket->TicketUser = array();
$ticket->fillTicket();
if(($this->CurrentUser['userid'] != $ticket->TicketUser['userid']) && ($_SESSION['al'] == 't' || $_SESSION['al'] == 'r')){
$ticket->setViewTicketHTML();
$title = "Error: You can only Edit items you have created.";
$_SESSION['vdaemon'] = '0';
} else {
$ticket->setEditTicketHTML();
$title = 'Edit ROI';
$_SESSION['vdaemon'] = '1';
}
break;
case 'ticket_submit':
var_dump('test');
exit();
$ticket->ticketid = (int)$this->Request['ticketid'];
$ticket->TicketUser['useremail'] = $this->Request['useremail'];
$ticket->saveTicket(0);
$_SESSION['vdaemon'] = '0';
if($_REQUEST['viewquick'] == 'quick'){$quick = 'ticket_quick_view';}else{$quick = 'ticket_filed';}
if($_REQUEST['edit'] == 'yes') {
echo 'works';
}else {
header("Location: ?view=".$quick."&ticketid=".$ticket->ticketid);
echo 'workss';
}
exit;
break;

I ended up fixing it and this is what the cause was.
Three things needed to be fixed.
The first thing was that I needed to fix a reference to a vdaemon library. The path was all screwed up. After that I got rid of a header redirect in two places. One was in a separate folder and one was on my index.php which I totally missed (thought I checked it). It was doing 302 redirects because the headers were there but in different folders throughout the server and on the index.php.
Next time I'll know what to look for. Thanks for everyone's efforts here. :)
Make sure you look for this code first when dealing with these errors
header("Location: http://www.foo.com");

Related

PHP Redirect to different URLs based on Parameters

I'm currently using this simple redirect (which passes all URL parameters to the next page) to redirect a link:
<?php
function preserve_qs() {
if (empty($_SERVER['QUERY_STRING']) && strpos($_SERVER['REQUEST_URI'], "?") === false) {
return "";
}
return "?" . $_SERVER['QUERY_STRING'];
}
header("Status: 301 Moved Permanently");
header("Location: https://example.com/" . preserve_qs());
?>
One of the issues is that I have with this method is that I need to create a separate file for each redirect.
Is it possible to make this into something that I simply add different URLs inside and based on URL parameter that I call, it sends people to the right URL.
For example, in PHP, we store 3 URLs and we assign them 3 values (parameters):
example1.com = page1
example2.com = page2
example3.com = page3
and the PHP file URL would look like this:
example.com/redirect.php?land=page1?restofparameters
keep in mind that the rest of the parameters need to be sent to the goal page, but not the page1 parameter which calls the URL inside the PHP file.
So the target URL that people will land will would look like this:
example1.com/?restofparamaters
Any help is appreciated. Thank you!
you can try .htaccess method... like this...
In .htaccess file
RewriteEngine On
RewriteRule ^test/(1)? http://triviamaker.com [R=301,L]
then for check this one
localhost/your_project_folder/test/1
Note:- that .htaccess file must be in Root directory of your Project.
& if you have any query or found any problem related to that .htaccess method then you can Search "redirect a specific url to another url with htaccess" in Google. you will Found more details easily about this...
hope this one is Helps to you... Thank You...
I would recommend a link parser.
But here goes an example based on your code for stripping and inheriting GET URL parameters.
<?php
function preserve_qs() {
if (empty($_GET)) return '';
return '?'.http_build_query(array_diff($_GET, ['land']));
}
http_response_code(301);
header('Location: https://example.com/' . preserve_qs());
exit;
?>
I write this in test.php ... just for your Reference
Run This File for check Output...
http://localhost/url_rewrite/test?2
and run also like this http://localhost/url_rewrite/test?land=2
echo $temp = $_GET['land'];
echo $temp = $_SERVER['QUERY_STRING'];
if($temp == "1" || $temp == "land=1"){
header('Location: https://example.com');
}else if($temp == "2" || $temp == "land=2"){
header('Location: https://gmail.com');
}else if($temp == "3" || $temp == "land=3"){
header('Location: https://apple.com');
}else if($temp == "4" || $temp == "land=4"){
header('Location: user/2');
}
I hope this helps to you... Thank you

Redirection HTTP/1.1 301 Moved Permanently

I have the following files. The objective of this is to redirect to the correct news. For example:
localhost/tostadotv/esto-es-una-noticia-28.html
If I intentionally modify the url, for example:
localhost/tostadotv/esto-es-una-noticia-modificada-incorrecta-28.html
I should redirect myself to the correct news:
localhost/tostadotv/esto-es-una-noticia-28.html
However, it redirects me to this:
http://localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/esto-es-una-noticia-28.html
Where this error? Could you please help me thanks. Excuse my english I'm from Argentina I do not speak English
.htaccess
RewriteEngine On
RewriteRule ^.*-([0-9]+)\.html$ noticia.php?id_not=$1 [L]
noticia.php
<?php require_once("lib/connection.php"); ?>
<?php require_once("lib/functions.php"); ?>
<?php
fix_category_product_url();
?>
functions.php
function fix_category_product_url() {
$proper_url = get_proper_category_product_url(1);
if ( SITE_DOMAIN.$_SERVER['REQUEST_URI'] != $proper_url) {
header('HTTP/1.1 301 Moved Permanently');
header('Location: '.$proper_url);
exit();
}
}
function get_proper_category_product_url($id) {
$product_id = $_GET['id_not'];
$query = sprintf('SELECT titulo FROM noticias WHERE id_not = "%d"', mysqli_real_escape_string($GLOBALS['DB'], $product_id));
$restit = mysqli_query($GLOBALS['DB'], $query);
$noticia = mysqli_fetch_array($restit);
$proper_url = make_category_product_url($noticia['titulo'], $product_id, $id);
return $proper_url;
}
define('SITE_DOMAIN', 'localhost');
function _prepare_url_text($string) {
$NOT_acceptable_characters_regex = '#[^-a-zA-Z0-9_ ]#';
$string = iconv('UTF-8','ASCII//TRANSLIT',$string);
$string = preg_replace($NOT_acceptable_characters_regex, '', $string);
$string = trim($string);
$string = preg_replace('#[-_ ]+#', '-', $string);
return $string;
}
function make_category_product_url($product_name, $product_id, $ido) {
$clean_product_name = _prepare_url_text($product_name);
if ($ido == 0)
$url = strtolower($clean_product_name).'-'.$product_id.'.html';
else
$url = SITE_DOMAIN.'/tostadotv/'.strtolower($clean_product_name).'-'.$product_id.'.html';
return $url;
}
As said in the comments, the final solution for the asker was to add http:// to the defined SITE_DOMAIN constant.
Before
define('SITE_DOMAIN', 'localhost');
After
define('SITE_DOMAIN', 'http://localhost');
But there's more to it than just that. Let's focus on the following two functions:
function fix_category_product_url(){
$proper_url = get_proper_category_product_url(1);
if(SITE_DOMAIN.$_SERVER['REQUEST_URI'] != $proper_url){
header('HTTP/1.1 301 Moved Permanently');
header('Location: '.$proper_url);
exit();
}
}
function make_category_product_url($product_name, $product_id, $ido) {
$clean_product_name = _prepare_url_text($product_name);
if($ido == 0)
$url = strtolower($clean_product_name).'-'.$product_id.'.html';
else
$url = SITE_DOMAIN.'/tostadotv/'.strtolower($clean_product_name).'-'.$product_id.'.html';
return $url;
}
The idea here is that $proper_url actually ends up getting a value from make_category_product_url() because its result is returned by get_proper_category_product_url(). It makes sense because make_category_product_url() has more parameters and uses the other to get their values.
What's funny about this is that the else block of the second function doesn't always return a path, but rather a URL. The problem here is that such URL is given without a defined protocol, but starts with the domain name instead. This value is therefore mistaken as a path.
Now take a look at the first function: it ultimately redirects the user using header('Location: '.$proper_url);. As we discussed earlier, $proper_url is not always a path, so the protocol should be added somewhere in the code whenever a URL takes place instead of a path. That's where the actual solution comes in: adding http:// where SITE_DOMAIN is defined is one way to do this, because this constant is only used when a URL takes place. There are many other ways to do this, but this one is completely valid.

Infinite loop php redirect based on cookie

I'm currenting busy coding a registration page. The page has three steps and every step has its own cookie value. What I'd like to do is checking for the cookies value and transfer the user to the correct page upon visiting the website
Example:
if the value of $_COOKIE['step'] is 'step_two' it should redirect to: www.domain.com/register.php?step=your_details. If the cookie's not set, it should not redirect and stay on the register.php page.
The redirecting is working 'fine', but it gets into an infinite loop. I really cant think clear anymore as I've been awake for almost 24h now. Therefor I would appreciate it if anyone could push me into the right directions.
Piece of code:
$cookie_value = 'step_2';
setcookie("step",$cookie_value, time()+3600*24);
$cookie_not_set = true;
$cookie_step_two = false;
if (isset($_COOKIE['step'])) {
if ($_COOKIE['step'] == 'step_2') {
$cookie_not_set = false;
$cookie_step_two = true;
header('Location: ?step=your_details');
exit();
}
} else {
$cookie_not_set = true;
}
Thank you.
Nowhere are you actually setting your cookie value, so it won't change. That's why you have an infinite loop.
$_GET and $_COOKIE have nothing to do with each other. It looks like you want:
if ($_GET['step'] === 'your_details')`
...which would be better than using a cookie anyway.
You are going to constantly enter your if condition as there is no other manipulations going on to your cookie data.
if your cookie is set to "step_2" you will enter the loop. No changes are in place, so on the refresh to the page. You will re-enter the step_2 condition and be into a redirect.
I'm also assuming that you understand that your $_GET & $_COOKIE requests are completely different. If not, see #Brads answer
A solution to stop this infinite loop would be:
if (isset($_COOKIE['step'])) {
if ($_COOKIE['step'] == 'step_2') {
$cookie_not_set = false;
$cookie_step_two = true;
$_COOKIE['step'] = 'step_3';
header('Location: ?step=your_details');
exit();
}
But also take note, your true/false validations/changes are local changes and will not be absolute on page refresh
I believe your issue is the redirect is not changing your cookie, so you need to look at the GET var you a re passing if the cookie is set to step_2 thus;
$cookie_not_set = true;
$cookie_step_two = false;
if (isset($_COOKIE['step'])) {
if ($_COOKIE['step'] == 'step_2') {
if( !empty($_GET['step']) && $_GET['step'] == 'your_details' )
{
... you have redirected and now can continue ...
}
else
{
// redirect and set the get var to signal to this script.
$cookie_not_set = false;
$cookie_step_two = true;
header('Location: ?step=your_details');
exit();
}
}
} else {
$cookie_not_set = true;
}

Redirect if a specific word is present in a php variable

I am making a web hosting website. I have a text field where a user can put in the domain name they want to register for example "domain.com" or "domain.info". I want to redirect the domains which have .info in their url to a different page for example www.example.com/info.php and domains which have .com in their url to www.example.com/com.php, is it possible to do that using php?
Your help will be highly appreciated.
Thanks
Yes it's possible.
For example your input is called name="domain"
in php you catch the post:
if($_SERVER["REQUEST_METHOD"] == "POST") {
$extension = end(explode($_POST["domain"]));
header("Location: ".$extension.".php");
}
This wil redirect the extension with .php
Be aware, this is not secure! But just a push in the right direction.
A more secure option:
if($_SERVER["REQUEST_METHOD"] == "POST") {
$extension = end(explode($_POST["domain"]));
switch($extension) {
case 'com':
$redirect = 'com.php';
break;
case 'info':
$redirect = 'info.php';
break;
default :
$redirect = 'unknown.php';
break;
}
header("Location: ".$redirect);
}
Something like this:
if(isset($_POST['domain'])){
$arr = explode('.', $_POST['domain']);
if($arr[1] == 'com'){
// redirect
}elseif($arr[1] == 'info'){
// redirect somewhere else
}
}
This is just a guideline. You should escape the $_POST data.

Check session and cookie not working in PHP

I have this code that makes sure your are logged in, and then making sure you are on the right page by checking a cookie set at login. This code works on a page in a directory underneath the login in script, however in a page in a directory below that it always takes you to accessdenied. Any ideas?
<?php
session_start();
if(!isset($_SESSION['SESS_MEMBER_ID']) || (trim($_SESSION['SESS_MEMBER_ID']) == '')) {
header("location: http://mywebsite.com/member/accessdenied.html");
exit();
}
$_COOKIE["verify"] = $verify;
if( $verify != file_get_contents("name.txt")) {
header("location: http://mywebsite.com/member/accessdenied.html");
} else { }
?>
And it seems like just the bottom part, the part that checks the cookie, isn't working. Again, any ideas?
I think you have your cookie assignment backwards:
$_COOKIE["verify"] = $verify;
Should be
$verify = $_COOKIE["verify"];
And that should be:
$verify = isset($_COOKIE["verify"])?$_COOKIE["verify"]:false;
As if the cookie was not previously set, well it would give a notice error.
<?php
$verify = $_COOKIE["verify"];
if( $verify == file_get_contents("name.txt")) {
echo $verify . 'is equal to the content of name.txt'
} else {
echo $verify . 'is NOT equal to the content of name.txt'
}
?>
Try debugging the code with this. See if the content of your variable is what you want. But I find it unusual that a variable would be a file.
are you sure you always get the content from file_get_contents? I could imagine it's found in one directory but not in the other!
antoher idea: cookies can be set to be relevant for a particular directory only. I just realize, what we're missing here, is the part where you set the cookie in the first place.

Categories