I made a bitly url shrinker, and I currently have a Soundcloud Javascript API that outputs a url link of a song. Im trying to shrink it using my shrinker. The shrinker works using this:
<?php echo $bitly->shorten('http://google.com'); ?> //Equals google.com in short url format
The javascript code I'm trying to implement it in is this: Ill go ahead and give you what I tried to do already, that didn't work.
Before I edited:
container.find('span.player-actions').html(
'Soundcloud | Download'
);
After I tried:
container.find('span.player-actions').html(
'Soundcloud | Download'
);
Any suggestions, I'm open to anything. And would love to make this work!
That has been already explained but in case you're new to this concept, there is a simplified explanation.
<?php tags in your code are processed on server before your page is sent to user's browser. Actually browser never receives those tags - they're replaced with PHP output on server and then the resulting page is sent to user.
As a result of some mistake sometimes PHP code makes into user's browser but it behaves as any other non-standard tag - content between <?php and ?> would be invisible to visitor.
JavaScript, on the other hand, operates in user browser with (in our case) what PHP has already output. When you change the page with JavaScript, it's not sent back to server - actually, server is totally unaware of that, so it can't execute the PHP code you're outputting by your JavaScript.
In order to achieve a similar result you need to send an AJAX request from your JavaScript code. It'll basically be another "page request" initiated by your JavaScript, but happening at the background with PHP output not replacing your current page, but arriving into your JavaScript code. This way your JavaScript is outputting PHP output and not PHP code, that's why it is possible.
You cannot call PHP on a string that is generated via javascript since PHP is server side and executed before JavaScript which is client side.
If you want to shorten this string, you'll have to make an ajax call to a php page that will return the shrunk url.
Related
For the first time ever I've tried to use file_get_contents() in a file (index.php):
echo $globalIP=file_get_contents("tools.php");
The contents of tools.php, for the time being, is as follows:
<?php
$ip=$_SERVER['REMOTE_ADDR'];
echo $ip;
?>
but when I run index.php it doesn't display anything at all.
Peculiarly, all info on the Internet about file_get_contents() pertains to the client side rendering, and not about what the server-side script should contain. I also experimented with stream_context_create() in tools.php to no avail.
By the way, the idea is to replace the URL in
$globalIP=file_get_contents("http://bot.whatismyipaddress.com");
with my own script. Whatismyipaddress returns a string containing the IP, nothing else, which is what I am looking for.
The answer was much more straightforward:
If a relative link is used, the php code of the actual file is displayed: file_get_contents("tools.php")!!!!
In order for it to work a full URL is needed, in my case:
file_get_contents("http://localhost/tools.php");
using JS : (in <head> tag)
<script>window.location="https://stackoverflow.com";</script>
using PHP : (in <head> tag)
header('Location: https://stackoverflow.com');
end();
Which one I should use ? or another ?
and what about using <meta>?
<meta http-equiv="refresh" content="0;url=https://stackoverflow.com"/>
Many good answers , I don't know which answer I will accept, Thanks so much
The result is same for all options. Redirect.
<meta> in HTML:
Show content of your site, and next redirect user after a few (or 0) seconds.
Don't need JavaScript enabled.
Don't need PHP.
window.location in JS:
Javascript enabled needed.
Don't need PHP.
Show content of your site, and next redirect user after a few (or 0) seconds.
Redirect can be dependent on any conditions if (1 === 1) { window.location.href = 'http://example.com'; }.
header('Location:') in PHP:
Don't need JavaScript enabled.
PHP needed.
Redirect will be executed first, user never see what is after. header() must be the first command in php script, before output any other. If you try output some before header, will receive an Warning: Cannot modify header information - headers already sent
A better way to set the location in JS is via:
window.location.href = 'https://stackoverflow.com';
Whether to use PHP or JS to manage the redirection depends on what your code is doing and how. But if you're in a position to use PHP; that is, if you're going to be using PHP to send some JS code back to the browser that simply tells the browser to go somewhere else, then logic suggests that you should cut out the middle man and tell the browser directly via PHP.
It depends on how and when you want to redirect the user to another page.
If you want to instantly redirect a user to another page without him seeing anything of a site in between, you should use the PHP header redirect method.
If you have a Javascript and some action of the user has to result in him entering another page, that is when you should use window.location.
The meta tag refresh is often used on download sites whenever you see these "Your download should start automatically" messages. You can let the user load a page, wait for a certain amount of time, then redirect him (e.g. to a to-be-downloaded file) without Javascript.
PHP redirects are better if you can as with the JavaScript one you're causing the client to load the page before the redirect, whereas with the PHP one it sends the proper header.
However the PHP shouldn't go in the <head>, it should go before any output is sent to the client, as to do otherwise will cause errors.
Using <meta> tags have the same issue as Javascript in causing the initial page to load before doing the redirect. Server-side redirects are almost always better, if you can use them.
The first case will fail when JS is off. It's also a little bit slower since JS must be parsed first (DOM must be loaded). However JS is safer since the destination doesn't know the referer and your redirect might be tracked (referers aren't reliable in general yet this is something).
You can also use meta refresh tag. It also requires DOM to be loaded.
window.location.href = 'url';
is beter than
header('location:url');
because the header command is mustly return an error "Warning: Cannot modify header information - headers already sent"
using js window.location.href = 'url';
this is beter
I want to get the URL using (preferably) PHP or JavaScript. The page was opened using an anchor name (e.g. index.php#aboutme). When I use
$host = $_SERVER['HTTP_HOST'];
$script = $_SERVER['SCRIPT_NAME'];
$params = $_SERVER['QUERY_STRING'];
it returns http://afterimagedesign.tk/index.php without the #home on the end. How can I get this?
PHP cannot ever get this hashtag (that's what it's called), because the browser never sends it to the server in any form.
JavaScript can access it with window.location.hash, but that's client-side.
What are the difference between server-side and client-side programming?
<?
echo parse_url("http://localhost/index.php#aboutme",PHP_URL_FRAGMENT);
?>
Output: aboutme
or with JS
window.location.hash
The only way to access this data with PHP would be to send it to PHP via an AJAX call when the page loads. As the other responses have said, the hashtag is never sent to PHP inside the original request. So you would have to send it afterwards.
If this is information you need, you would have to change your application to not use hashtags and instead append this data to the query string (ie index.php?home rather then index.php#home and manually scroll the page using javascript when it loads based on this.
I have a situation like this.
<php>
<redirect the page >
<exit>
<javascript>
<redirect the page again>
I want to have javascript that basicall disables the PHP redirect. So if Javascript is enabled on the browser, the javascript redirect will work, if it disable, the PHP redirect will work. Should I just enclose the PHP code in span and make it invisible? Any ideas?
Addition ok this is not a simple redirect. the form authentication is rather odd. Register.php -> register_submit.php -> Was there an error -> yes go back to register.php (everything is javascript at this point). What I have added is PHP authentication as well so if I see javascript is not enabled, I take the user to register.php *after it does the regular checking of fields *.
PHP is a server-side technology. By the time Javascript even sees what's happened, it's too late.
Short answer, JS can't intercept/block PHP (as long as PHP is being called first).
Order of events:
Client requests page
PHP executes and generates output of page
Browser receives output
Browser begins parsing what was sent by what PHP already spit out.
Remove your PHP redirection and add this in your <head>:
<noscript>
<meta http-equiv="refresh" content="0; http://www.example.com/1" />
</noscript>
<script>
window.location = 'http://www.example.com/2';
</script>
This will redirect to http://www.example.com/1 when javascript is disabled, and to http://www.example.com/2 when it's enabled.
PHP code is executed on the server-side, while JS is client-side. So with that structure the PHP will kick in before the JS is executed. If you want JS to control PHP you need to make use of AJAX to control it.
Also enclosing PHP code in a "span" won't have any effect.
Javascript and PHP do not directly interact (exceptions apply, don't worry about them now :D). The best way to implement this type of interaction between these two disparate languages is to use the query string or cookies.
I think there may be some confusion here about when and how PHP is executed as opposed to when and how javascript is executed. Think of PHP as the factory - the goods are physically produced there. Think of your server as the loading dock, the internet as the shipping company. Your browser is the store, HTML is the shelves; Javascript is the window decorations on the store that sells the merchandise. The window decorations have no affect on the production, the factory can make some window decorations, but it doesn't use them, it just ships them right along with the merchandise for the store to use. PHP is the factory, javascript is the decoration. There are some problems with taking this analogy too literally, but there it is in a nutshell.
You can make the PHP redirect conditional on the presence or absence of a specific query string variable:
<?php
// redirect if $_GET['no_redirect'] is NOT set. Reverse the true/false to invert this rule
$do_redirect = (isset($_GET['no_redirect']) === false ? true : false);
// perform the redirect, if required
if ($do_redirect === false)
header('Location:http://mydomain.com');
?>
Javascript:
window.location = 'http://mydomain.com/?no_redirect=1';
EDIT If you're trying to detect if javascript is enabled, then the best way is for javascript to set a cookie if it is enabled. PHP can then check for this cookie, and if it isn't found then you'll know that javascript didn't get a chance to set it, so it must be disabled (or the user edited their cookies).
Take a look at some code snippets for dealing with cookies in javascript, and check out the documentation for dealing with cookies in PHP.
I want to post to an external php file and get the result. It a php that i have hosted in my server online. I want the static page in my localhost post by ajax and load the html in a div. But I'm not able to do this.
$.post("http://www.site.com/index.php", { font: "panchami", input: "hi" } );
Is there anything wrong in this?
The Same Origin Policy prevents Ajax calls to external domains.
Popular workarounds include
JSONP
Embedding the data in an iframe instead
Using a server-side proxy the does the fetching (see #BrunoLM's answer for a PHP example; it is possible in any server-side language)
YUI's Get as shown in #Alex's answer
depending on what your use case is.
Javascript doesn't allow cross domain requests.
What you can do is a PHP file on your server that reads the contents of the other site:
<?php echo file_get_contents($_REQUEST['url']); ?>
Then make requests to your file, like so:
$.post("proxy.php?url=external_url", ...);
Or using GET, for example:
http://developer.yahoo.com/yui/get/
This kind of request is dangerous, it is called a Cross-Site request and is forbidden by most browsers. If you look in your error console you should see a message to that effect.
If you really have no alternative then you can consider using iframes, the src attribute can be outside the current domain and you can parse the information using javascript.
Hope that helps :)