Can I redirect two pages in a single COMMAND? - redirect

I want to redirect two pages at once.
if(any statement) {
Header("Location: http://example.com/page.php");
Header("Location: folder/page.php"); }
else { echo " comment "; }
Will this work?
Found a solution see on comments.

No. Why on Earth would you want to? Even if you could, how do you think a browser can display the two pages? New windows/tabs? But what if it's a windowless browser (like lynx)?
If you use the above code, PHP will replace the first location header with the second as no headers are actually sent until you start outputting content, after which you cannot call header().

Related

Can't send both get and post http requests via html-forms on perl

I have switch-structure on my site. I work with get requests and I load different scripts, depending on what was in the query. There are switch-instruction in script.
For example.
Link: site.com/ - printing something like 'press some menu button'
Link: site.com/?mode=users - loading script users.pl
Link: site.com/?mode=address - loading script addreses.pl
Link: site.com/?mode=users&action=add - loading script users.pl and in this script I has another switch which do dif things depending on get-parameter 'action'.
On _/?mode=users&action=add_ I has form. Action in this form is none (action=''), method=post. So when I press 'send' button this page must be just refreshed with post-parameters yet. Right? Right.
The problem: when I press send button index.pl is loading, not same page. Like there are no get-parameters.
I try to print all parameters, which page receiving
#names = $params->param;
print #names;
It's printing post-parameters, which I entered in form, but not printing get-parameters from link. Link is same : /?mode=users&action=add
I tried to print $ENV{'QUERY_STRING'}; - all parameters shown. Both post and get and their names are correct.
Whats the problem? $ENV{'HTTP_REQUEST'}; is only 'post' when I send form, but 'get' when I just loading page with form.
Perl can't send both get and post requests? What I have to do?
This is how I work with query string:
$params=new CGI;
$action=$params->param("mode"); # or $action=$params->param("action");
And switch:
use feature 'switch';
for ($action) {
when ('users') {
do "./pages/users.pl";
}
.... some more 'when'
default {
print 'select something';
}
}
Try with out the slash infront of the question mark
eg change
/?mode=users&action=add
to
?mode=users&action=add
I don't really know why it hadn't work, but I found next solution. For example link is /?mode=users&action=add (its a form). I just adding 2 to form:
<input type='hidden' name='mode' value='users'>
<input type='hidden' name='action' value='add'>

Finding broken links with Selenium Remote Driver

I have a site with login and i want to test all links present in that site.
I tried with finding links and click on each to verify with Selenium Remote Driver. But one problem i have is coming back to previous URL and selecting next link. This testing should be recursive.
How can we do this with Selenium Remote Driver?
Following program i tried to check broken links
sub traverse {
my ($self) = #_;
my $links = find_links("//a");
foreach my $index (1..$#$links) {
my $url = $links->[$index]->get_attribute('href');
my $result = $links->[$index]->click();
if ($result) {
traverse();
} else {
print "url is broken $url\n";
}
}
}
I know it's possible to do in C# by checking the returned status code. So you don't actually click on the link, but you are retrieving the header of the response that link is going to give. In this header you can find the HTTP Status Code which you can check to see if the link is giving a valid response or not. Plus you're not leaving the current site!
In C#, a possible method to get the status code will look like this (The checking of the HTTP status code is not included):
private static HttpStatusCode GetStatusCode(string url)
{
var result = default(HttpStatusCode);
var request = WebRequest.Create(url);
request.Method = "HEAD";
HttpWebResponse response;
try {
response = request.GetResponse() as HttpWebResponse;
} catch (WebException) {
return HttpStatusCode.NotFound;
}
if (response != null)
{
result = response.StatusCode;
response.Close();
response.Dispose();
}
return result;
}
Altough this is no Perl code, I hope this helps
Why are you not trying to use some tool, because your site can has over 9000+ urls, it's a lot of time and job, you can use Xenu
Install
In option check use Cookie
Run IE and login thorugh it
Run Xenu
P.S. To test privete part of your site, you must login thorugh IE because Xenu uses only IE cookie
Hmm, I've crossed this bridge before and here is how I solved it. Now I should say that I crossed this bridge before WebDriver :) so this is using WWW::Selenium instead of S:R:D but the concept is the same and still applies.
One of the most tedious tasks, IMO, for a test engineer, is manually verifying links. We can automate most of the process and as long as we have the URL's for where we are expected to land after clicking the link, we can verify this functionality using Selenium and a little bit of JS.
In the below example we first navigate to our desired website and then use Selenium's getEval() function to execute JavaScript that gathers all the links on the page (anchors) and saves them in a comma separated list. This list then gets split and pushed into an array. We then iterate through the list of links in the array clicking on each one and then navigating back to the starting page using go_back.
use strict;
use warnings;
use Time::HiRes qw(sleep);
use Test::WWW::Selenium;
use Test::More "no_plan";
my $sel = Test::WWW::Selenium->new( host => "localhost",
port => 4444,
browser => "*iexplore",
browser_url => "http://www.google.com/");
$sel->open_ok("/", "true");
$sel->set_speed("1000");
my $javascript = "var allLinks = this.browserbot.getCurrentWindow().document.getElementsByTagName('a');
var separator = ',';
var all_links_texts = '';
for(var i = 0; i < allLinks.length; i++) {
all_links_texts = all_links_texts+separator+allLinks[i].href;
}
all_links_texts;";
# Get all of the links in the page and, using a comma to separate each one, add them to the all_links_texts var.
my $link_list = $sel->get_eval($javascript);
my #link_array = split /,/ , $link_list;
my $count = 0;
# Click on each link contained in the array and then go_back
# You can add other logic here like capture and store a screenshot for example
foreach my $link_name (#link_array) {
unless ($link_name =~ /^$/){
$sel->click_ok("css=a[href $= $link_name]");
$sel->wait_for_page_to_load("30000");
print "Clicked Link href: $link_name \n";
$sel->go_back();
$count++;
}
}
print "Clicked $count URL's";
pass;
This can be easily modified to do much more than just click on the links. And of course nothing beats a good pair of eyes on the intended landing pages for the links clicked. Implementing a similar solution in your organization might ease with the manual testing. Here is how I have done it in the past:
Not everything can be automated, but we can certainly make it much easier to review large amounts of links. The above logic can be easily extended to capture a screen shot and add it to a queue of "to be reviewed" images. These properly tagged [by the software] images are what you use in the final phase of the test; visual verification phase.
With this approach you'll know right away if a link is broken or not (assuming you update the logic above to also include this, again this example can be easily extended to include that functionality). As well you will have the capability of visually verifying the screen shots of the intended link landing pages.
I actually have a blog post about this very same issue here: get all links and click on each one
Hope that helps.

How to redirect from one CGI to another

I am sending data from A.cgi to B.cgi. B.cgi updates the data in the database and is supposed to redirect back to A.cgi, at which point A.cgi should display the updated data. I added the following code to B.cgi to do the redirect, immediately after the database update:
$url = "http://Travel/cgi-bin/A.cgi/";
print "Location: $url\n\n";
exit();
After successfully updating the database, the page simply prints
Location: http://Travel/cgi-bin/A.cgi/
and stays on B.cgi, without ever getting redirected to A.cgi. How can I make the redirect work?
Location: is a header and headers must come before all ordinary output, that's probably your problem. But doing this manually is unneccessarly complicated anyways, you would be better of using the redirect function of CGI.pm
Use CGI's redirect method:
my $url = "http://Travel/cgi-bin/A.cgi";
my $q = CGI->new;
print $q->redirect($url);

How can I redirect an address starting with #!

I have a new wordpress site instead of my old Wix one.
in the old one there were page addresses like http://example.com/#!contact/ct07/ in the new one this page resides under http://example.com/contact
I've tried 3 redirections plugins but none works (Redirection, redirection editor, quick 301 redirect).
I have no access to the .htaccess file
On redirection it seems like the engine does not see the URL.
Any manageable idea besides JS ? I don't want to miss google juice
Browsers don't send the part after # to the server, so the server don't know about this part and won't be able to do the redirect.
So you have to do the redirection in javascript:
if (/^#contact\//.test(document.location.hash)) {
document.location = '/contact';
}
For SEO purpose, you may want to handle the _escaped_fragment_ parameter too
I had this problem a few weeks ago.
PHP does not get anything after hash tag, so it is not possible to parse request url and get hash. But JavaScript can.
Below you find my solution for WordPress redirects by hashtag #! :
(You should put this code in functions.php of your active theme):
function themee_hash_redirects() {
?>
<script type="text/javascript">
function themee_hashtag_redirect( hashtag, url) {
var locationHash = document.location.hash;
if ( locationHash.match(/#!/img) ) {
if ( hashtag == locationHash ) {
document.location.href = url;
}
}
}
// Examples how to use themee_hashtag_redirect
themee_hashtag_redirect('#!contact', '/qqq/');
themee_hashtag_redirect('#!zzz', '/aaa/');
</script>
<?php
}
add_action('wp_footer', 'themee_hash_redirects');

Drupal 7, form submit: Display result of a query?

Using the form api, I'm to display the result of some processing after a post is sent by a form.
So I implement the module_name_submit(...) method:
function cmis_views_block_view($delta = '') {
$cmisview = cmis_views_load($delta);
$block['content'] = cmis_views_info($cmisview);
return $block;
}
The processing is very simple. The call happens properly and I would like to display the $block on screen.
Returning this doesn't help. Maybe a redirect could help, but I'm looking first for something equivalent to drupal_set_message() but that dispays html in the page.
Any leads welcome :-)
Thanks,
J.
You probably need to use $form_state['redirect']
Put the content in a session variable, redirect to that page and have a custom block output that specific variable in the $content