Control existence and appearence of a banner on different sites - php

We have a banner. This banner is located on different sites-partners. We don't have admin access to these sites. The banner has to be on each site. But how to control this?
It can be done manually, but it would be great if there were ways to do it automatically (especially when the list of the sites is huge).
The banner has to be visible. It means that it's not enough to just check HTML-code on each of the sites. JavaScript and CSS have to be taken to consideration because one can easily hide the banner using these instruments.
I think it's a common task, but I couldn't find anything on the web. Any thoughts and links would be appreciated.

In short: no way. That means - you can not be sure that banner will 100% appear for each visitor of your partner's web-site.
Why it is impossible?
First, such thing as AdBlock - yes, even if all is going fine, most people don't want to see ads, so they are free to use any tools to disable it. Important note: AdBlock will check request URI before sending request & retrieving content from server, so it's working scheme is 'prevent before loading', not 'load & then hide'. That means - if your banner will be added to blacklist - no request for it will be done from client.
Ok, let us suppose that we've left all client-side stuff alone and dealing only with partner's web-site. I.e. from there we're assuming that client do not have 'evil' things that prevents your banner from appearing:
What can we do?
At first glance, using PHP there's HTTP_REFERER field in $_SERVER environment variable. Using it you can check which web-site requested your banner. Let it be an image, so you can easily perform your check:
function checkReferer($referer)
{
//this could also be retrieved from config or DB:
$clients = [
//client name with available referrers:
'Lorem Ipsum' => ['page1_address.php', 'page2_address.php']
];
//get client:
$client = key(array_filter($clients, function($referrers) use ($referer)
{
return in_array($referer, $referrers);
}));
//perform some DB updates, which set state of $client
}
//some stuff
checkReferer($_SERVER['HTTP_REFERER']);
//sending image:
$image = '/path/to/image.png';
header('Content-Type: image/png');
readfile($image);
exit();
How it works? It will retrieve web-page address, from which your banner.php script was requested. Not all clients will send this header (I mean HTTP_REFERER - it's non-mandatory), but in most cases it will contain valid address. In this case you may rely on this since it's your partner interest that you will get that properly (because you're checking them). So: when a visitor will open your partner's web-site, your script will be requested. Example:
<!-- somewhere on partner's side in HTML: -->
<img src='http://your-site.com/banner.php'/>
Alternative: you may want to have even more solid condition. Then you'll need to assign some unique code for each partner and tell to it that code. Your image will look like:
<img src='http://your-site.com/banner.php?code=codeOfThisPartner'/>
-and in PHP you'll just have 1:1 relationship between partners and their codes:
function checkPartner($code)
{
//this could also be retrieved from config or DB:
$clients = [
'codeOfThisPartner' => 'Lorem Ipsum',
//...
];
//get client:
$client = $clients[$code];
//perform some DB updates, which set state of $client
}
//the rest is the same, code will be in $_GET['code'], obviously
What's wrong with this?
If we're supposing that client's browser is not disabling ads, then method above will work only for those cases, when partner's web-site is not hiding your banner. As you've mentioned, there could be javascript that hides your banner. They could, for example, perform a call of your script and then hide it in hidden block. So - this is a way to filter only good partners.
What other ways can be suggested?
You can try to request your partner's pages with cURL, for example. Since you've mentioned that you'll have many partner's pages with your banner, you'll better to use curl_multi_* to increase speed of your queries. Algorithm will be:
Request partner's web-site
Check if your banner exist in HTML code. You can, for example, ask your partner for strict form of placing your banner (may be even some partner's code inside banner's link)
What about JavaScript?
Common answer: nothing. There are million ways to spoil your banner. There are also million ways to hide that fact, that something in html/javascript is spoiling your banner (.eval() in javascript, yes). And there's no way you can check that automatically unless you'll write full browser emulation with javascript engine & html renderer (and even there it will be hard to answer a question 'will that banner appear on the right place of screen'). If you don't trust your partner, then, unfortunately, you'll have to check that site manually - i.e. you'll 100% sure only with checking by your own eyes.
Side note
You should also keep in mind that different web-browsers would handle markup in different ways. So in some cases it may be not a case of 'evil partner, hiding your banner', but a 'stupid browser which does not support feature "X" or functionality "Y" e t.c.'. May be it could be also treated as a problem on your partner's side, but to make that clear - you should tell him, which browsers must display the banner correctly.
So, what to do?
Impossibility of full automatic check does not means that you should not do anything. You can perform some sorts of checking, like in suggestions above - and, I think. that will divide your partners into 2 parts: those, who passed your check and those, who failed. So, at least you'll not have to check first part manually - thus, it's a way to save your time. Second part could be checked only manually to be 100% sure - and each specific case should be handled separately so you'll be sure that it's 100% fault of your partner.

Related

Automation Testing with Ranorex

I am testing my PHP web application using the Ranorex automation tool. Whenever I record my test case, it is recording perfectly but when I play my test case it stops (sometimes it hangs) at the middle and fails my test case.
What is the reason that is creating this issue?
The good way to automate is to write code without using recorder.
There are some reasons of it:
recording unnecessary steps
saving elements to repository by wrong
attribute. As usual there are automation id and regex of text or
caption of element. For dynamically changeable elements this doesn't
fit.
different types of elements requires different types of methods
to wait and validate them.
In this way, I recommend you to watch the screencast of Ranorex Team.
There are 5 videos about everything you should know about UI Mapping.
According to you exceptions and errors in comments:
"...ATagPrintReport3' does not match the specified value (actual='False', expected='True')"
It means that Ranorex recorded element ATagPrintReport3 with value of some bool attribute. Of cource, the value of element will change, so the right way is to identify element with the attribute that will never change (uniqueId, name, class and other).
Failed to find item 'updtaed_cpt_imsRepository.OpeningInventoryStockValueJpg.ButtonOK'. No element found for path '/form[#title~'^Opening\ Inventory\ Stock\ V']/button[#accessiblename='OK']' within 1m.
This error can be the result of very fast dynamically changeable element, the wrong path of element, it could be presented in dom but could not be visible.
The best way to know what's wrong is to use breakpoints and step-to-step debuging.
In my opinion the path for ButtonOK will be better if it looks like:
'/form[#title~'Opening\ Inventory\ Stock']/button[#name='OK']'
No element found for path '/form[#title='Reading Untagged Document']/?/?/button[#text='&Cancel']' within 1m.
It is the same to p.2
In this example, the path will be better:
'/form[#title='Reading Untagged Document']//button[#name='&Cancel']'
I am sure there is a error message in the report.
What does this message say?
I suggest checking if the elements can be recognized uniquely by Ranorex and if the webpage is fully loaded.
You can use the following code snippet in order to wait until the webpage is fully loaded.
WebDocument webDocument = "/dom[#caption='Your Test Page']";
webDocument.WaitForDocumentLoaded();
Concerning your second question, a video how to work with data connector can be found on the Ranorex website directly:
Data-Driven Test Automation with Ranorex

How to use php to store and display any visitor's input

I want to create a very simple site, but unfortunately my php skills are weak. Basically, when a user shows up, I want to have a page with text and a blinking cursor (I can probably figure the cursor part out myself, but feel free to suggest). When a user types, I want it to show the text as they type, and when they hit enter (or click something/whatever), the text just typed will be sent to a database and then the page will update with that new text, for anybody else to see. The cursor will then be blinking on the next line down. So basically it's like a really simple wiki, where anyone can add anything, but nobody can ever remove what has been typed before. No logging in or anything. Can someone suggest the best way to go about this? I assume it will require a php call to the database to display the initial page, then another php request to send data, then another php request to display the new page. I just don't know the details. Thanks so much!
Bonus question 1: How can the page be updated dynamically, so if A sends text while B is typing, B sees the text A sent on B's page immediately?
Bonus question 2: What sorts of issues might arise if this database grows extremely large (say, millions of words), and how might I address these up front? If necessary, I could show only a small chunk of the (text-only) database on any given page, then have pagination.
If you only have one page, you don't need a database. All you need to do is save a text file on the server (use fopen() and related functions) that only gets appended to. If you have multiple pages, then a simple id (INTEGER), filetext (LARGEBLOB). (Note largeblob has a limit of 2^32 bytes).
For the user's browser part, you'll need to use Javascript and AJAX to inform the server of any updates. Just get in touch with a PHP script that (1) accepts the input and (2) appends it to a file.
Bonus question 1: How can the page be updated dynamically, so if A sends text while B is typing, B sees the text A sent on B's page immediately?
Also use the AJAX call to fetch new content (e.g. if you assign line numbers, then the browser just tells the script the last line it read, and the script returns all new lines past that point).
I assume it will require a php call to the database to display the initial page, then another php request to send data, then another php request to display the new page.
Pretty much. But only send the last 50 lines or so of the file when the browser visits it. You don't want to crash the browser.
Bonus question 2: What sorts of issues might arise if this database grows extremely large (say, millions of words), and how might I address these up front? If necessary, I could show only a small chunk of the (text-only) database on any given page, then have pagination.
Think in terms of bytes, not words, and you'll likely run into performance issues. You could cap file sizes or split up the storage into multiple files at a certain size so you don't have to scan pass content that will rarely be fetched.

Trouble with implementation of download code redeemer

I'm designing a website for a small indie record label and they've dropped a bombshell asking if I could implement a function where a user can enter a code to receive a digital download.
Is there a simple solution to doing this? I was thinking all I would need is an input field where the user can enter a code, it gets verified and then allows a download but it sounds too simple. Is this even possible with something like .php (complete beginner)?
I'm willing to learn or I would've packed it in already so any advice would be great. Thanks.
Edit:
Thanks to some great advice I was able to create this!
If you wanted to do it at a very simple level, it is not much more than you describe it to be. You would need to familiarize with PHP and MySQL or some other database, but it isn't too difficult to create.
You need to figure a few things out, such as how do you want to limit the codes, 3 downloads in the first 24 hours to allow for failed downloads, restrict it to IP, or strictly one full download. Since you have the list of the 1000 codes given, you will probably want to base your system around having codes pre-generated and inserted in the database, rather than having an algorithm that can validate the codes on the fly and then check for already used codes.
You would want to store the download(s) in a directory that is not accessible from the web, and the php script would validate the code, and if valid serve the download to the user.
You can probably look to the wordpress plugin's database structure for other ideas, but I think at the very least you would need:
download_code (the code itself, probably primary key or at least index)
download_file (optional, the name/path of the file this code allows them to download)
redeemed (0 if not redeemed, 1 if redeemed)
redemption_time (timestamp of the first, or last redemption based on your requirements)
download_count (how many times downloaded if allowing more than 1)
ip_address (ip address of the redeemer)
email_address (email address if you want to collect it, or give user the option)
download_url (the unique string for the download url. this could be one way to implement download verification beyond the code, but is not necessary)
You would then need to create an html page with the text box for entering the code, and any other optional data you wish to collect. The form would submit to your PHP script.
When the PHP script receives a form post, it would validate all of the data (i.e. email address if you were collecting it). Once all data is valid, you read from the database looking for a code matching what the user entered.
If no data was found with the code, send them back to the form to try re-entering it. If a record is found, you can check the redeemed value from the database and see if the code has been used or not. If it has, this is where you can use custom logic to decide if they are still within their download window, the ip address is the same, or whatever criteria you want to use to allow re-downloads.
If it has been redeemed, show an error message. If it is still okay to download, you can serve a download by reading the file and sending it to the browser see example #1 here.
At some point you will have to update your database to set the redeemed flag to 1 and update the other values such as timestamp and download count. You can either run this code before you serve the download, or you can run it after the download is served. In some cases if the download was cut off, the last portion of your script won't run and therefore won't update redeemed or download_count. This may or may not be what you want, so you can decide where you want to do the updating.
Eventually you can update it to include an administration panel, but in the beginning all configuration could be done within the php script or config file. And eventually you could update it to use flash or some other technology to download the file and show progress bars etc.
Hopefully that will give you some idea on whether or not you want to try to implement it. Otherwise you could always search php on Hotscripts to see if there is an already rolled standalone version of what you want.

Create redirect URL

Here is the real case, in the NewsNow.co.uk, there are many link of uptodate news from thousands of website. And the example for one of the news url:
http://newsnow.co.uk/A/471722742?-19721
all the news url are formated like that, but when we click it, we will be brought to the real url, for ex:
http://www.abcactionnewsx.com/dpp/news/state/bla-bla
anyone know how to achieve this efficiently ?
Store a table of 'internal' paths (the 'newsnow' urls) and the 'destination' urls in a database of some sort; sqlite3 would be a fine choice for smaller applcations.
You could hash the 'internal' paths if lookup time for specific strings was too slow in the database you chose.
When a request comes in, look it up in the database and send back a 302 response with the 'target' URL as the new location for the resource.
This is done using a rewrite engine that is built into common webservers like Apache or Nginx. These engines allow you to write rules that transform a url like the first one into something that would be better understood by your php pages. For example, you could create rules that would turn your first link above into:
http://newsnow.co.uk/index.php?catagory=A&id=471722742&referrer=-19721
This is transparent to the user and they will only ever see the link they first typed in. You can then use the variables being passed in to perform whatever actions you desire. In this case you might want to use the variables to perform some kind of database lookup to retrieve the actual destination that you are interested in. Then it's just a question of performing a php redirect to the link in question.
Check out the following link for a very quick intro to Apache's rewriting capabilities (called mod_rewrite): http://www.besthostratings.com/articles/mod-rewrite-intro.html

Is is possible to parse a web page from the client side for a large number of words and if so, how?

I have a list of keywords, about 25,000 of them. I would like people who add a certain < script> tag on their web page to have these keywords transformed into links. What would be the best way to go and achieve this?
I have tried the simple javascript approach (an array with lots of elements and regexping/replacing each) and it obviously slows down the browser.
I could always process the content server-side if there was a way, from the client, to send the page's content to a cross-domain server script (I'm partial to PHP but it could be anything) but I don't know of any way to do this.
Any other working solution is also welcome.
I would allow the remote site add a javascript file and using ajax connect to your site to get a list of only specific terms. Which terms?
Categories: Now if this is for advertising (where this concept has been done a lot) let them specify what category their site falls into and group your terms into those categories. Then only send those groups of terms. It would be in their best interest to choose the right categories because the more links they have the more income they can generate.
Indexing: If that wouldn't work, you can maybe when the first time someone tries to load the page, on your server index a copy of it and index all the words on their page with the terms you have and for any subsequent loads you have a list of terms to send them based on what their page contains. ideally after that you would have some background process that indexes their pages with your script like once a day or every few days to catch any updates. Possibly use the script to get a hash of the page contents and if changed at all you can then update your indexed copy.
I'm sure there are other methods, which is best is really just preference. Try looking at a few other advertising-link sites/scripts and see how they do it.

Categories