Here's a head-scratcher:
I have a Dotnet Application which signs a user out after a certain inactive time period. A JavaScript function using an action defined in the relevant CSHTML sends the user to a certain controller method which will sign them out.
When the JavaScript code decides that the user should be signed out, it uses the following line to do so:
location.href = settings.actions.expireSession + '?returnUrlString=' + currentUrl;
where settings.actions.expireSession is defined as:
expireSession: '#Url.Action("Expire", "Session")'
and the return url string getting into the location.href looks like this:
http://localhost:49574/Report/ReportWithUserIdAndCaseId?userId=84&caseId=173
which is correct, and the entire string assembled together with the url action looks like this:
"/Session/Expire?returnUrlString=http://localhost:49574/Report/ReportWithUserIdAndCaseId?userId=84&caseId=173"
I set up a breakpoint at the point of entry in the relevant method, but what arrives in the string parameter named "returnUrlString" there is missing the "caseId":
http://localhost:49574/?returnUrl=http%3A%2F%2Flocalhost%3A49574%2FReport%2FReportWithUserIdAndCaseId%3FuserId%3D84
consequently, when I enter my username and password to log back in, I get redirected to the following url:
http://localhost:49574/Report/EntrySummaryReportWithPatientIdAndVisitId?userId=84
which fails because it's missing a crucial parameter.
Have I missed anything obvious? Is there something else in the automated background of Dotnet's addressing/redirection system that could be contributing to this mysterious disappearance?
A huge thanks to everyone for reading this, and a gargantuan thanks for contributors!
- Ilia
You need to escape the Url before it is sent to the Session/Expire page.
See this previous question for information on encoding it in Javascript:
Encode URL in JavaScript?
Once you have returnUrlString on your Session Expire page, you should then unescape it and direct the user to it.
The problem is that the Url to the Session/Expire page would be:
"/Session/Expire?returnUrlString=http://localhost:49574/Report/ReportWithUserIdAndCaseId?userId=84&caseId=173"
What the server sees is:
Page: "/Session/Expire?, QueryString ReturnUrlString: returnUrlString=http://localhost:49574/Report/ReportWithUserIdAndCaseId?userId=84, QueryString CaseId: &caseId=173"
It is interpreting your &caseId as part of the /Session/Expire URL. This is why it disappears.
Related
I am trying to complete the freecodecamp project- url shortener in glitch.
glitch project link: https://glitch.com/edit/#!/url-sh0rtn3r
What i am able to do currently is:-
inside a POST request take the url to be shortened, convert it to a shorturl, add both to a database, and
inside a GET request search for the shorturl(appended to the url rootpath) and redirect to the original url...
the shortened url for https://www.freecodecamp.com
becomes https://url-sh0rtn3r.glitch.me/api/shorturl/9575 where 9575 points to the original url i.e. https://www.freecodecamp.com
Now i was wondering if i can somehow shorten the url further to be something like https://initialpart/api/shorturl/9575
But im stuck trying to figure out how to access the initialpart as a parameter inside a request where the path is pointing to whatever comes after initialpart.
You can get the "initialpart" (domain) from your request object with req.headers.host. In your case this would return "url-sh0rtn3r.glitch.me" as a string. You can then use that in your app however you like. You can also get the rest of the request URL path with req.url, which in the above example would return "/api/shorturl/9575" as a string.
I am trying to fetch my current page URL, for an if-else statement.
My URL is like this : http://something.com/fetch_url.php#filter=.apples
And my code is :
<?php
echo $_SERVER['REQUEST_URI'].'<br/>';
?>
It gives me :
/fetch_url.php only, but i need to check for #filter=.apples , in if-else case.
Please guide.
Try using a correct URL like http://something.com/fetch_url.php?filter=apples
# part in the URL never approach to a web server, therefore you cannot access it.
And use if statement like this.
if($_REQUEST['filter']=='apples'){
//then perform your action here according to requirements
}else{
// otherwise provide instruction for the else condition here
}
A # part in the URL can never reaches a web server, hence you cannot access it. One workaround that we have been using is to use Javascript to access it via window.location.hash, and then do a separate post to the server to obtain this hash, if it's necessary.
Similar question has been asked before: Get fragment (value after hash '#') from a URL in php
I'm trying to submit a form with Java, which looks like this:
<form id="bForm" action="/a/b.php" method="POST"></form>
There are no <input type="hidden">, there are not even any <input> at all.
In the source, I see that this form is submitted when I click a link which
executes javascript code like this:
javascript:b(<k>,<v>)
where <k> and <v> is some key and value consisting of some numbers.
Now, the function b() in the source is this:
function b(value, key) {
var form = document.getElementById('bForm');
form.action = '/b/user/' + key + '/' + value + '.txt';
form.submit();
}
What I'm trying to receive is the .txt file send as response, but trying to directly open
http://<url>.com/b/user/<key>/<value>.txt
in a browser does not get me the .txt file, but a message saying it has to be accessed by using the POST method.
Now, I read about how to send requests via POST (also from trying to find a
solution here) and think I understood most. Still, I only read about mostly everything today, including what forms are, so excuse me if the solution is obvious.
The problem I have here is that there are no parameters I can pass to "let the URL know" which action to do upon submitting the form.
Now as I can probably not change the action of the form, what do I do?
Can I somehow "connect" to the URL which leads to the .txt file via "POST"?
Or do I need to execute the javascript somehow, and if so how?
Edit: I have solved the problem now, not sure if my explanation was not good enough, but the solution seemed pretty simple, I just didnt know enough about forms.
So, action=#myurl# means that the form is submitted to the #myurl#, not to the current page. And, although the target URL seems to point to some file which cannot do anything with the submitted form, this is not right. In fact, it points to a script, but the script is "hidden" in a way that we only see the path to the file we want to download in the URL.
So, I just did this:
URL url = new URL("http://myurl.com/b/user/" + key + "/" + value + ".txt");
HttpURLConnection c = (HttpURLConnection) url.openConnection();
//set User-Agent, Cookie, etc. properties here
c.setRequestMethod("POST");
c.setInstanceFollowRedirects(false); //not sure if this is needed
InputStream in = c.getInputStream(); //this submits the parameter-less form
//now I can get the response with this stream, i.e. read & save the .txt file
All tested and running complete code now, so it works for sure.
We are adding a video to the home page of a site, and want to be able to automatically pop up the video (in a lightbox-style container) whenever the #video tag is present in the URL:
http://www.domain.com#video
The video needs to pop up if a link is clicked internally on the site (ie: <a href="#video">) and also if the hash is present in the URL on page load.
Easy enough to check for the hash in the URL using window.location.hash or when a link with the hash is clicked and fire the associated javascript function. That's working as expected without any issues.
However as this URL will be sent out in emails with Google Analytics code automatically added, the analytics code is appended to the end of the URL:
http://www.domain.com#video?utm_source=...
Because the analytics code will change with each email campaign, I can't do a simple Javascript replace() to ignore the analytics code.
How do I go about checking whether the hash is present in the URL, but ignore anything after a ? if present?
Isn't the proper form of a URL to have the hash at the end after the query parameters? Then, the location.hash variable will work properly and you won't need special code. Your URL should look like this and then you can just directly use location.hash which will be #video:
http://www.domain.com?utm_source=xxx#video
I don't advise this as the solution (I think you ought to get the URLs fixed to be legal), but you can use this code to parse the hash value out of the URL even if it's in the illegal position:
var matches = window.location.href.match(/#(.*?)(\?|$)/);
if (matches) {
var hash = matches[1];
}
This code extracts from the "#" to either end of string or "?" whichever comes first. You can see it run on a bunch of test URLs here: http://jsfiddle.net/jfriend00/HuqL7/.
location.hash.match(/[^?]*/)
Assuming the hash is always first, that should do it.
(This is a literal answer to your question, but there is a huge caveate) Technically, you can just test:
var h = window.location.hash;
var ind = h.indexOf( '?' );
var test = ind <0?h:h.substr(0, ind)
If you want Google Analytics to work, you may have problems #. The rule is that everything after a # is not sent to the server, which means that your Analytics may go out the window. You need to make sure that your hash is added after all of the Google stuff. If it is, then you won't need to worry about testing anything.
I made a bookmark that users can add and it sends them to my site capturing the referrer.
Bookmark
My problem is that for some reason the location.href part instead of printing http:// it prints: "http%3A//". I want to remove it and get just the domain.com
I have a similar code that maybe could be useful but I'm having a hard time figuring out how to implement it inside HTML.
// Function to clean url
function cleanURL(url)
{
if(url.match(/http:\/\//))
{
url = url.substring(7);
}
if(url.match(/^www\./))
{
url = url.substring(4);
}
url = "www.chusmix.com/tests/?ref=www." + url;
return url;
}
</script>
Thanks
In most browsers, the referrer is sent as a standard field of the HTTP protocol. This technically isn't the answer to your question, but it would be a cleaner and less conspicuous solution to grab that information server-side.
In PHP, for example, you could write:
$ref = $_SERVER['HTTP_REFERER'];
...and then store that in a text file or a database or what-have-you. I can't really tell what your end purpose is, because clicking a bookmark lacks the continuity of browsing that necessitates referrer information (like the way that moving from a search engine or a competitor's website would). They could be coming from a history of zero, from another page on your site or something unrelated altogether.
Like already stated in my comment:
Be aware that this kind of bookmarking may harm users privacy, so please inform them accordingly.
That being said:
First, please use encodeURIComponent() instead of escape(), since escape() is deprecated since ECMAScript-262 v3.
Second, to get rid of the "http%3A//" do not use location.href, but assemble the location properties host, pathname, search and hash instead:
encodeURIComponent(location.host + location.pathname + location.search + location.hash);