PHP to JS

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
a204801Asked:
Who is Participating?
 
Dave BaldwinFixer of ProblemsCommented:
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
 
Ray PaseurCommented:
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
 
a204801Author Commented:
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
a204801Author Commented:
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
 
Dave BaldwinFixer of ProblemsCommented:
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
 
a204801Author Commented:
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
 
Dave BaldwinFixer of ProblemsCommented:
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
 
a204801Author Commented:
Here is where I'm confused.  The parameters are still in the URL in the browser.  They don't disappear.
0
 
Dave BaldwinFixer of ProblemsCommented:
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
 
a204801Author Commented:
Right, so I guess my questions is why cant the parameters be extracted from URL if they are still visible?
0
 
Dave BaldwinFixer of ProblemsCommented:
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
 
a204801Author Commented:
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
 
Dave BaldwinFixer of ProblemsCommented:
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
 
Ray PaseurCommented:
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
 
a204801Author Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.