Solved

PHP to JS

Posted on 2013-11-04
15
376 Views
Last Modified: 2013-11-08
I have the following code in PHP and was wondering if someone could help me translate to JS:

<?php
$host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : NULL;
if ($host == 'aa.website.com')
{
    $newURL = 'http://anotherweb.com';
}
elseif 
(  isset($_GET['utm_medium']) 
&& isset($_GET['utm_source']) 
&& isset($_GET['utm_campaign']) 
&& isset($_GET['utm_term']) 
) 
{
    $newURL = sprintf('https://wow.website.com/%s-%s-%s-%s/start?utm_source=%s&utm_medium=%s&utm_term=%s&utm_campaign=%s'
    , strip_tags($_GET['utm_source'])
    , strip_tags($_GET['utm_medium'])
    , strip_tags($_GET['utm_campaign'])
    , strip_tags($_GET['utm_term']) 
    );
} 
else 
{
    $newURL = 'https://wow.website.com/pscweb-free/start?utm_source=psc&utm_medium=website&utm_term=subpage&utm_campaign=linkage';
}

?>

Open in new window


What Im trying to do with this code is say if you are coming from a particular URL (aa.website.com) go to this $newURL, but if not and there are 4 UTM's then grab the UTMs parameters and put them in the URL string (https://wow.website.com/%s-%s-%s-%s/start).  At the same time keep the existing UTM parameters in the URL.  Also, if there aren't any UTMs then just go to this $newURL (https://wow.website.com/pscweb-free/start?utm_source=psc&utm_medium=website&utm_term=subpage&utm_campaign=linkage).

If anyone could help me, it would be much appreciated.

Thanks, Mac
0
Comment
Question by:a204801
  • 7
  • 6
  • 2
15 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39622192
if you are coming from a particular URL (aa.website.com)
The place the client is "coming from" is usually in HTTP_REFERER, not HTTP_HOST.

Why isn't this working for you?  Why do you want to convert it to JS?
0
 

Author Comment

by:a204801
ID: 39622216
I need to convert because the hosting does not allow this, apparently when the request reaches there "edge" layer, those parameters are stripped out so that a single page can be cached even when there are multiple campaign parameters.  The net result is PHP doesn't "see" those parameters, since before the request reaches PHP they are stripped.

So the only way to do this would be to use JS, as far as I can tell.
0
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 39622273
That sounds like bad hosting to me.  Here is a simple program that can show you all of the information that the server passes on to you.  Upload it to your hosting and look at it.  Then add the parameters you are using to the URL and do it again to see what shows up.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>PHP Server Variables</title>
</head>
<body><h1>PHP Server Variables</h1>
<?php

//reset($_SERVER);
foreach($_SERVER as $key => $value) {
    echo "<b>$key :</b> $value<br />\n";
}

print_r ($_POST);
?>
</body>
</html>

Open in new window

0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 

Author Comment

by:a204801
ID: 39622329
yeah, as expected:

USER : 565d0e6a8e1c4788bf4e95f396c8c803
HOME : /srv/bindings/565d0e6a8e1c4788bf4e95f396c8c803
FCGI_ROLE : RESPONDER
QUERY_STRING : q=content/test-page&utm_medium=PANTHEON_STRIPPED&utm_source=PANTHEON_STRIPPED&utm_campaign=PANTHEON_STRIPPED&utm_term=PANTHEON_STRIPPED
REQUEST_METHOD : GET
CONTENT_TYPE :
CONTENT_LENGTH :
SCRIPT_NAME : /index.php
REQUEST_URI : /content/test-page?utm_medium=PANTHEON_STRIPPED&utm_source=PANTHEON_STRIPPED&utm_campaign=PANTHEON_STRIPPED&utm_term=PANTHEON_STRIPPED
DOCUMENT_URI : /index.php
DOCUMENT_ROOT : /srv/bindings/565d0e6a8e1c4788bf4e95f396c8c803/code
SERVER_PROTOCOL : HTTP/1.1
GATEWAY_INTERFACE : CGI/1.1
SERVER_SOFTWARE : nginx/1.4.2
REMOTE_ADDR : 174.46.233.162
REMOTE_PORT : 38637
SERVER_ADDR : xxx.xx.xxx.xxx
SERVER_PORT : 10978
SERVER_NAME :
REDIRECT_STATUS : 200
SCRIPT_FILENAME : /srv/bindings/565d0e6a8e1c4788bf4e95f396c8c803/code//index.php
PATH_INFO :
PATH_TRANSLATED : /srv/bindings/565d0e6a8e1c4788bf4e95f396c8c803/code
HTTP_HOST : xxx-xxxxx-xxxxxx.gotpantheon.com
HTTP_USER_AGENT : Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:25.0) Gecko/20100101 Firefox/25.0
HTTP_ACCEPT : text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
HTTP_ACCEPT_LANGUAGE : en-US,en;q=0.5
HTTP_X_PANTHEON_STYX : 1
HTTP_SURROGATE_CAPABILITY : styx="ESI/1.0"
HTTP_ACCEPT_ENCODING : gzip
HTTP_X_BYPASS_CACHE : 1
HTTP_COOKIE : has_js=1; Drupal.toolbar.collapsed=0; Drupal.tableDrag.showWeight=0; SESS190fea890b262b7ceab618a4488825d6=Ci9cdueSGFzi4UtY4NlxtrmEov_y5MABB8h32qewHDc
HTTP_X_FORWARDED_FOR : 174.46.233.162, ::ffff:127.0.0.1,::ffff:127.0.0.1
HTTP_X_VARNISH : 1969449643
HTTP_X_PANTHEON_CLIENT_IP : 174.46.233.162
HTTP_X_FORWARDED_PORT : 52544
HTTP_X_FORWARDED_PROTO : http
HTTP_CONNECTION : keep-alive
PHP_SELF : /index.php
REQUEST_TIME : 1383594051
PRESSFLOW_SETTINGS : {"conf":{"pressflow_smart_start":true,"pantheon_binding":"565d0e6a8e1c4788bf4e95f396c8c803","pantheon_site_uuid":"fc6fdb8e-6087-44e7-b6ab-8d5d7b56affa","pantheon_environment":"dev","pantheon_tier":"live","pantheon_index_host":"index.live.getpantheon.com","pantheon_index_port":449,"redis_client_host":null,"redis_client_port":null,"redis_client_password":null,"file_public_path":"sites/default/files","file_private_path":"sites/default/files/private","file_directory_path":"sites/default/files","file_temporary_path":"/srv/bindings/565d0e6a8e1c4788bf4e95f396c8c803/tmp","file_directory_temp":"/srv/bindings/565d0e6a8e1c4788bf4e95f396c8c803/tmp","css_gzip_compression":false,"js_gzip_compression":false,"page_compression":false},"databases":{"default":{"default":{"host":"10.178.139.38","port":10269,"username":"pantheon","password":"a9d746c6f9c249fdaf26121f3a12e400","database":"pantheon","driver":"mysql"}}},"drupal_hash_salt":"a9d746c6f9c249fdaf26121f3a12e400","config_directory_name":"config"}
PANTHEON_ENVIRONMENT : dev
PANTHEON_SITE : fc6fdb8e-6087-44e7-b6ab-8d5d7b56affa
HTTP_REFERER :
Array ( ) 

Open in new window

0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39622388
That's pretty clear.  The HTTP_REFERER is blank also so you can't tell where the request came from.  Is this for a Google ad campaign?
0
 

Author Comment

by:a204801
ID: 39622438
Yes, we have SEM ads directing traffic to our site.  We then pass on this info in two ways to our app.  Just trying to maintain the lead parameters.

On all pages of my site I have a link to our app .  If I have the following URL from SEM:

http://www.mysite.com?utm_source=psc&utm_medium=website&utm_term=subpage&utm_campaign=linkage

when someone clicks on the link to the app, I want the URL to be to

http://app.mysite.com/psc-website-subpage-linkage/start?utm_source=psc&utm_medium=website&utm_term=subpage&utm_campaign=linkage

IF there are no UTM parameters, I have a generic URL to the app as follows, kind of a default URL unless there are 4 UTM parameters:

http://app.mysite.com/free-start/start

Am I making any sense?
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39622548
Yes, but it's clear that on that hosting it's not going to happen.  Using javascript to use AJAX to send the parameters will encounter the exact same thing.
0
 

Author Comment

by:a204801
ID: 39622570
Here is where I'm confused.  The parameters are still in the URL in the browser.  They don't disappear.
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39622652
Why would they disappear?  You can send the link in a page to the browser.  When you click on the link, it appears in your address bar.  The request with that query string is sent to the server.  The filtering is then done on the server in the request that is sent.
0
 

Author Comment

by:a204801
ID: 39622673
Right, so I guess my questions is why cant the parameters be extracted from URL if they are still visible?
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39622755
I guess you didn't understand that it is being done on the server After the browser sends the request.  What you see in your browser URL is what is sent to the server.  When the server receives it, the server does the filtering.  When the server has made the changes, it is passed to PHP.  

The software that runs the server can take your request and change it to anything it wants.  're-writing' is something that is often used on servers to change the URL that is received from the client to something that can be used by the software on the server.
0
 

Author Comment

by:a204801
ID: 39622799
I guess Im just overall confused.  

All I want is to be able to extract the parameters of the URL with JS.  ARe you saying thats not possible?
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39622860
Where would you run javascript to get the parameters?  It would have to be in the client's browser.  You still have to send them to the server.  How are you going to do that?  If you send them the same way as a query-string, they are going to be filtered just like they are now.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39623972
Maybe this article will add some understanding of the division of labor between client and server.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/A_11271-Understanding-Client-Server-Protocols-and-Web-Applications.html
0
 

Author Comment

by:a204801
ID: 39634803
Thanks everyone, I went with a different solution, see below.

<script type="text/javascript">
		function getURLParameter(name) {
		  return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20'))||null
		}

		function getURL() {
			var newurl = "http://app.mysite.com/web-free/start";
			if (window.location.hostname == "partner.mysite.com") {
				newurl = "http://othersite.mysite.org";
			} else {
				utm_source = getURLParameter('utm_source');
				utm_medium = getURLParameter('utm_medium');
				utm_campaign = getURLParameter('utm_campaign');
				utm_term = getURLParameter('utm_term');
				if (utm_source && utm_medium && utm_campaign && utm_term) {
					newurl = "http://app.mysite.com/"+utm_source+"-"+utm_medium+"-"+utm_campaign+"-"+utm_term+"/start?utm_source="+utm_source+"&utm_medium="+utm_medium+"&utm_campaign="+utm_campaign+"&utm_term="+utm_term;
				}
			}
			//alert("redirecting to "+newurl);
			window.location = newurl;
		}	
		</script>

Open in new window

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Start Auto-download of File when page loads? 1 23
Ajax and PHP 9 31
issue with DB import 1 20
Use Select Query to Return Results as a Form 9 24
This article discusses how to create an extensible mechanism for linked drop downs.
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question