Load different PHP files based on JavaScript availability - javascript

I currently have the code below (PHP)
<?php
require_once __DIR__.'/includes/Connect.php';
require_once __DIR__.'/includes/Nojs.php';
require_once __DIR__.'/includes/Main.php';
require_once __DIR__.'/includes/footer.php';
?>
I want Main.php to load only if JavaScript is enabled, and Nojs.php to load only is JavaScript is disabled.
Surrounding Nojs.php with tags does nothing (See below code)
<?php
require_once __DIR__.'/includes/Connect.php';
?>
<noscript>
<?php
require_once __DIR__.'/includes/Nojs.php';
?>
</noscript>
<?php
require_once __DIR__.'/includes/Main.php';
require_once __DIR__.'/includes/footer.php';
?>
Since I know the PHP cannot access the browser as it is done server-side, is it possible to try to use HTML (Like I attempted) or other JavaScript to get it to work?
Thanks in advance.
EDIT:
Creating a new file for nojs.php and using meta refresh works, but there is a noticeable delay. The page starts loading the original URL and then redirects to the other. A visitor can quite easily cancel the page load process and stop the page load before the redirect. They will have a partly loaded page, but all the content they really need will be there. Is there a way to force an immediate redirect (Yes, "content" is set to "0" per the below anwser)?

Create another PHP file with no js version when first PHP loaded it will check if no js support will redirect to no js version of PHP
<noscript>
<meta http-equiv="refresh" content="0;url=nojs-version.php">
</noscript>
Edit: It is also mentioned by W3C as technique without confusing user
https://www.w3.org/TR/WCAG20-TECHS/H76.html

Related

PHP open link and then execute function

I recently started building a login system using php and mysql database.
Right now I have already built this using php code and data typed by user in a bootstrap modal.
User opens his mailbox, click a link which executes verification script in database to set user as verified and load my main site page.
After that I would like to add some modal to tell something like "Your account was verified" just once. What is a right way to do it? I thought that it can be done by php by running jquery function located in index.php like
<?php
header('location:index.php');
echo "<script> $('#addText').modal();</script>";
?>
But as I can see it is not correct and php just loads index.php and that's all.
P.S. I am sorry for my ignorance of php fundamentals.
If you use an HTTP redirect, the contents of the page are ignored.
Instead, you can use a meta-refresh tag, which allows delaying the redirect.
<head>
<meta http-equiv="refresh" content="5;url=https://example.com/index.php" />
<script src="js/jquery.js">
<script> $(function() {$('#addText').modal();})</script>
</head>
5; means to delay the refresh for 5 seconds.

How to load a new website which url is stored in an array every minute

I need to load a different website to my browser every minute. The sites` urls are saved in an array.
I don't want to use the sleep function because it stops the script.
Currently the first page won't load. How do you do this with java script or with cron jobs?
<?php
$pages = ["http://www.google.com", "http://www.amazon.de"];
foreach ($pages as $page) {
header("Location: $page");
sleep(5);
}
Actually it should load google.com and after a minute it should load amazon.com.
You can't. HTTP doesn't work like that.
You can redirect from the requested URL to a different URL, but then the browser will have gone to the new URL and it will be too late to send a different redirect from the original URL.
The only way I can think of to do what you want it to display the cycling pages in a frame and use client-side JavaScript to change its src on an interval. That would only work if the pages being displayed didn't block framing with X-Frame-Options (which http://www.google.com does).
Yes, This can't be achieved only using PHP you have to use javascript to reload the page at client side
Following code might help you, where you have te redirect to same page with array index and then loading webpage in iframe.
<?php
$i = isset($_GET['index']) ? $_GET['index'] : 0;
$pages = ["https://www.google.com", "https://www.amazon.de"];
echo $i ."--". count($pages);
if($i < count($pages)) {
?>
<iframe src="<?php echo $pages[$i];?>"></iframe>
<script type="text/javascript">
setTimeout(function(){ alert("Hello");
window.location.href = "http://localhost/reload.php?index=<?php echo ++$i;?>"; }, 30000);
</script>
<?php
} else {
echo "All Done."
}
?>
Hope this helps you !!!
PHP is server side. It means that when client connects to your server with a browser, your PHP script generates a content to display and does it only once per request.
Javascript is client side. It means that your browser runs the script. But after changing url of the currently viewed site, you won't have any power on it, as it loads own javascript scripts (if there are any).
What You want to do could be achieved via frames (but the sites you have listed are not frame friendly, and they will redirect to itself anyway, so you will lose control on first load) or via browser plugin, that is more complicated and user would have to install the plugin first.

How to include PHP $_SESSION values in a javascript file?

I use $_SESSION['siteRoot'] to store the root address of my website in (it's basically a framework so this can change depending on the URL used to access the site). I need to use this value in some of my javascript files...
Up until now I've been including my js files as .php, and putting the following code at the top of my js files, like so:
<?php
header("Content-type: application/javascript");
session_start();
?>
This has been working fine on my local-host for testing - but when I upload it to the live server I noticed that my session was being reset every time I reload the page, and after a day of debugging finally discover that it's the session_start(); line in my java-script files that is causing this behavior.
I've tried the following:
if (!isset($_SESSION))
{
session_start();
}
if (session_id() == '')
{
session_start();
}
if (session_status() !== PHP_SESSION_ACTIVE)
{
session_start();
}
and also just leaving out the session_start altogether. If I don't start the session then I can't use the variables (obviously...), but I can't find a way of starting it that doesn't wipe the session I already created in my main page.
Any ideas?
The problem was not caused by the session itself exactly, but because I hadn't included my class autoloader before calling the session, and so my custom classes were not surviving the deserialize process (even though it's a javascript file and I don't use any of said classes!)
I changed my javascript code to:
<?php
include ('include/autoloader.php');
header("Content-type: application/javascript");
session_start();
?>
and everything works fine. Easy to forget when you're not using any of the serialized classes on that page!

PHP Ajax jQuery cross domain file loading

So before you say this can't be done. These are all files that I have one server. I just have some of them listed under different domains.
My PHP script will access all the files but when I try to do an ajax request to try to load the file I will often get an error (because the site i am accessing is secure and the one I am accessing it through isn't).
What I need is a way to have php grab the file. But I need aJax to retrieve the file and render it for me. I am also using ACE editor to edit the file
The bit of code I have here will actually error out as well because it will load and print out the file where $page is defined but won't load where htmlspecialchars is.
<script>
var e = ace.edit("editor");
<?php
$page = readfile($_SERVER['DOCUMENT_ROOT'].$_GET['dir']);
echo 'e.setValue('.htmlspecialchars($page, ENT_QUOTES).');';
?>
</script>
I have an ajax get request working but it doesn't work when I go to a directory with a special htaccess file. Now I can't change the htaccess file (unless there is a way for me to confirm that it is my script running and not someone else.
The question is, how can I access those other files without getting that error? Mind you those files could be extension. It is not limited to just scripts or css, mostly they will be html or php files.
After an hour of searching the deep dark depths of the php.net site I was able to put together a solution that works.
<?php
echo htmlspecialchars(
addslashes(
file_get_contents(
$_SERVER['DOCUMENT_ROOT'].$_GET['d‌​ir']
)
)
); ?>
the addslashes is the extra part that I needed. Then I also had to put it between the div for the editor. I couldn't use the editor.setValue() function.

ZF2 Redirect from phtml file

is there a way to make a redirection from inside a phtml file in ZF2 Framework?
The reason is that I am using the jQuery $.POST mechanism (ajax) to refresh a part of the page, but I cannot make a redirection from the controller used by the ajax because it will load the new page into the div (which is inside another page).
I have tried using ZF2's :
$this->_redirect($this->url('restaurants'));
But I get a ServiceNotFoundException. I also tried php's:
<?php header( 'Location: http://www.yoursite.com/new_page.html' ) ; ?>
But this just did not work at all.
Hope this is clear enough!.. Any help is much appreciated!
You can't use header when you printed anything before it. There must not be any output before header. So this means the exact same for $this->_redirect().
What you could do is the following:
Add this to your head section
<meta http-equiv="refresh" content="0; url=http://example.com/">
or use javascript to redirect the user. If you don't want to extend your layout file you should use javascript:
<script type="text/javascript">
window.location = "http://www.example.com/"
</script>
EDIT
After rereading your question i think the only possible way for you to redirect the user after you've received the page content is the javascript solution.

Categories