Rerouting URL in PHP to a JS function - javascript

I feel like there's an easy answer here but i can't seem to figure it out. Basically there's a URL in our app that's password protected. It reads from a text file in PHP. The $plainTextPasswordPath below is just a variable that gets read from a settings file. Below is the PHP.
<li><a id="help" target="_blank"><?=l(111)?><? echo file_get_contents($plainTextPasswordPath) ?>
And here's the JS
var domain = /:\/\/([^\/]+)/.exec(window.location.href)[1];
$(help).attr("href", "https://app-help" + '.' + domain + "/help")
}
Basically what it needs to do is reroute to this where it either authenticates you or loads your password in from your log in.
https://app-help:PASSWORD#SUBDOMAIN.url.com/help
Desired result "Where PASSWORD is the string loaded from the plaintext file and SUBDOMAIN is the current subdomain the user is accessing."
However all I am getting is this https://app-help.local.url.com/help. I am not sure if the # symbol needs to be in the URL - but I don't think this is allowed?
Thanks

Related

How to pull token from wordpress url and give it a variable/value?

Let me explain exactly what i'm trying to do.
Someone lands on https://crypto.enzlo.com and submits their email. They get taken to the thank you page.
The thank you page url contains a bunch of tokens that looks like this:
https://crypto.enzlo.com/apply/?contactId=103&inf_contact_key=d5e172efa53b9940a898afcfa25596e21f32e5a885a1411d8c094e77aedca3ba&inf_field_BrowserLanguage=en-US%2Cen%3Bq%3D0.9&inf_field_FirstName=&inf_field_Email=heiko%40viceoffers.com&inf_4dAXudNU8407Jeuy=
I need to pull the 'inf_field_Email' token from the url and put it into a image tracking url that needs to fire so I can track the signup and the email. The image iframe pixel i need to fire on this thank you page looks like this:
you'll notice "track_id=inf_field_Email" in the iframe url. I need track_id to post the email back to me. So the variable/token inf_field_Email should be showing the email address from the thank you page url. So in this case the inf_field_Email would be heiko#viceoffers.com
I am thinking I will need to concatenate it into the url using JS, by giving it a variable/value. But not sure how to do that or how to then put that variable into the iframe image pixel that needs to fire on the same page...
Please let me know what you guys think is the best approach for this. I have been able to post the inf_field_Email as text on a page and have it display the email using a wordpress plugin but that plugin couldn't transfer that token into the iframe url.
We have to convert the URL into an object so we can call the properties. We can do accomplish this by using JSON.parse, and then concatenating the property to the iframe src, below is an example of what I would do.
$(document).ready(function() {
var search = location.search.substring(1);
search = JSON.parse('{"' + search.replace(/&/g, '","').replace(/=/g,'":"') + '"}', function(key, value) { return key===""?value:decodeURIComponent(value) });
console.log(search.inf_field_Email);
document.getElementById("govice").src = "https://govice.online/track/goal-iframe?goal_id=466&track_id=inf_field_Email=" + search.inf_field_Email;
});

php dynamic page content based on url

So, there are 3 urls:
example.com/first
example.com/middle
example.com/last
In my sql db, there is table with each terms that correspond to related posts:
ID NAME POSTS
1 first 12,3,343
2 middle 23,1,432
3 last 21,43,99
So if an user visits example.com/first, then I want to show posts of "12,3,343" and other posts based on what url they are visiting.
Now, this is the sequence how it would work in my head:
User types "example.com/first"
js (ajax) or something detects the url (in this case, detects "first").
the term is sent to php query.
Gets appropriate info then sends it out (either by ajax or something else).
Am I approaching this right?
How does the server detects what url was requested? I supposed I can skip the first two steps if I know how the server detects the url and I can query the correct info directly instead of relying on js to detect it.
Thanks!
When you mention ajax, I assume you are not navigating away from the page your are on. Am I correct?
If so, you have to create another php file to respond to the requests:
A request is sent to file.php with the url as a query string
In file.php, let it query the DB and json_encode the data.
Retrieve the data and update the fields without navigating away.
PHP is only executed once (Server-side). if you want to execute another query you have to either navigate to other URL or just send your request to a php file via ajax.
You can get the segments of a url request using below statements
$url = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$segments = explode('/', $url);
Now you have all the segments in an array ($segments)
print_r($segments) to get the index of the segment you require.
Now compare that segment with your value
For Eg :
if( $segments[2] == 'first')
{
//Your Piece of code
}

Good way to have multiple URLs to same site and serve slightly different content?

Take 2 websites :
website1.com
website2.com
Both owned by the same company, lets say they're swimming pools in different towns.
Both domains both point to the same server, which is website1.com. The server has one page on it.
PHP and JavaScript are available.
What the client wants : if someone goes to website1.com, the top
picture should be of the water slides. If someone goes to
website2.com, the top picture should be of the giant turtle.
My method so far :
Since website2.com is a redirect, I make it redirect to website1.com?source=2.
1) I use javascript to look at the URL.
2) If source is in it, then I place a cookie source=2.
3) Then on every page, I look at the cookies.
4) If there is a cookie with source=2, then a variable is set in javascript, like poolLocation=2. Default value is poolLocation=1.
5) Then, wherever the content might be location specific, like in our top picture, I use javascript to test what poolLocation is with an if/else.
//$(function() {...
if(poolLocation === 2) {
$('#variableImage1').attr('src', 'img2.png');
} else {
$('#variableImage1').attr('src', 'img1.png');
}
<img id='variableImage1' />
Thoughts?
If both domains are actually being allowed to hit the PHP code (there's no redirect happening in an .htaccess file), I would recommend checking $_SERVER['SERVER_NAME'] for the domain and switching the value of some PHP variables (e.g. an image $src variable) before the code ever hits the front end. That would be my approach.
Just be careful to allow the user to stay on the domain they came from in this case - don't redirect them to any particular domain, and use relative URL paths.
If you're using apache, you could add a ServerAlias directive like ServerAlias website2.com in the config file of the website1.com, that way people accessing from website2.com will see the same content, later using PHP you could know which domain is people accessing from and therefor filter part of the content.
i.e.
<img class="main-image" src="images/<?php echo $_SERVER['HTTP_HOST'] == 'website1.com' ? 'water-slides.jpg' : 'giant-turtle.jpg' ?>"/>
or, if you prefer doing it on JS:
<script>
var domain = '<?php echo $_SERVER['HTTP_HOST'] ?>';
$(document).on('ready',function() {
$('.main-image').attr('src','images/'+(domain == 'website1.com' ? 'water-slides.jpg' : 'giant-turtle.jpg')');
});
</script>

Why is php not removing cookie set by javascript?

There is post comment box on my site.
Posting comment is handled by javascript, js posts data to php script and php does the db related stuff and shows confirmation only if user is logged in if user isnt logged in then php gives not_loggedin response after receiving it js shows the bootstrap pop over box with link to login page.
So if user is not logged in then the javascript stores the entered comment in cookie so that after logging in user dont have retype the comment. Like this
document.cookie = id + "=" + input_text + "; ";
and after logging in the comment textarea is populated by reading the cookie which has the stored comment text..
everything up to this is working perfect but after inserting the comment in php i am trying to remove the cookie like this .,
setcookie($id, "", time()-3600);
print_r($_COOKIE);
exit('<p class="bg-info">Thank you! Your comment has been posted.</p>');
but its still not removing cookie , when i reload the page , the comment textarea box i populated with the previously enetered comment which is read again from cookie.,
how do i solve this ?
i even tried displaying the cookie in php which is stored by js , lke this
//insert comment in db
//setcookie($id, "", time()-3600);
print_r($_COOKIE);
exit('<p class="bg-info">Thank you! Your comment has been posted.</p>');
but it doesnt shows the cookie which is set by the js, it shows PHPSESSID cookie after posting comment.,
Array
(
[PHPSESSID] => c5rc6c8ggg24edg1v2o8hebb20
)
i am not trying to remove the PHPSESSID cookie.,
i am showing this on page using js . as
post_comment.php is another file in another directory on the same server .
what i am doing wrong ?
In simple words ,
js is setting cookie and php should remove cookie.
----------
update 1 :
i tried setting path while setting cookie in js like this
document.cookie = id + "=" + input_text + "; path=/";
and after posting cookie., now i get this.,
Array
(
[PHPSESSID] => c5rc6c8ggg24edg1v2o8hebb20
[4778] => this is comment
)
my php code is like this .,
//insert comment is db
setcookie($id, "", time()-3600);
printr($_COOKIE);
exit('<p class="bg-info">Thank you! Your comment has been posted.</p>');
but cookie is still there.
update 2 :
this is very strange.,
if i try to set the cookie of the same name in php , one more cookie gets created.
my php code.
setcookie($id, "sdf", time()+36000);
printr($_COOKIE);
exit('<p class="bg-info">Thank you! Your comment has been posted.</p>');
now when i check the broswers cookie manager , i see 2 cookies with the same name .,
but both having different content , the one which was set usng javascript have the comment enetered by user and another cookie which we set using php above , is having content "sdf".
i dont know how is this possible to be having2 cookies with the exact same names. ,
any clues ?
You need to make sure the all the
parameters (except name and time depending on the cookie.) are same while Setting Cookie in Javascript and while Removing Cookie in PHP
Parameters i.e. name,path (value and expire time can be different.)
for eg.
While setting cookie in javascript if you use it like this
document.cookie = id + "=" + input_text + " ; path=/";
you set the path to "/"
then while removing cookie in php you should specifically set like this.
//remove cookie.
setcookie($id, "", time()-36000 , "/");

Check certain URL in JavaScript?

How can I add something in JavaScript that will check the website URL of someone on a web site and then redirect to a certain page on the website, if a match is found? for example...
the string we want to check for, will be mydirectory, so if someone went to mysite.com/mydirectory/anyfile.php or even mysite.com/mydirectory/index.php JavaScript would then redirect their page / url to mysite.com/index.php because it has mydirectory in the URL, how can I do that using JavaScript?
If I have understood the question correctly, then it is fairly simple and can be achieved using document.URL
var search = 'mydirectory'; // The string to search for in the URL.
var redirect = 'http://mysite.com/index.php' // Where we will direct users if it's found
if(document.URL.substr(search) !== -1) { // If the location of
// the current URL string is any other than -1 (doesn't exist)
document.location = redirect // Redirect the user to the redirect URL.
}
Using document.URL you can check anything in the URL, however you might want to look into using something like Apache's mod_rewrite for redirecting the user before they even load the page.
Check out window.location, particularly it's properties and methods. You would be interested in (part of the) pathname property (you can split it on /) and the href property to change the page.
This is all assuming the javascript is being served in the first place; so I'm assuming anyfile.php and index.php would all result in the JS being served and not some 'generic 404' message.

Categories