I have a page named Index And a page chat
for check session write the code for print echo session->user;
this code work in chat.php but include chat.php in index.php echo session->user; not work
e:g
//index.php
<?php
<iframe src="./chat.php"></iframe>
enter code here
?>
//chat.php
<?php
include_once ('./session.php');
echo $session->user;
?>
chat.php printed user but index.php not print
Where is the problem?
Please help
While this problem is in Firefox not in IE problem
For me, this works for iframe, start the session like this:
header('P3P: CP="CAO PSA OUR"');
session_start();
in chat.php and index.php remember to include
session_start()
unless it is automatically started in your php.ini
session_start()
Related
I have a hyperlink and clicking on that link will run a JavaScript function. I also have a PHP variable $counter. Inside the JavaScript function, the value of $counter is increased by 1 i.e., $counter++. It works fine. But the same function also runs whenever the page is refreshed. So the value of the $counter is increased by 1 whenever the page is refreshed. I tried all the solutions available on the net like preventDefault(), clickevent handler etc., But nothing works. Please help to fix this. In the below code, I have set $counter as 0. But it loads with 1 as output. I want it to count only when the hyperlink is clicked. Here is my sample code.
<?php
session_start();
require("dbconfig.php");
$counter=0;
?>
<html>
<head>
<script>
function onclick() {
<?php
$counter++;
?>
}
</script>
</head>
<body>
link text
</body>
<?php
//tracing counter value
echo $counter;
?>
</html>
TL;DR:
You can't mix PHP and JS code expecting that JS function will execute PHP code.
First PHP prepares output to browser, then Browser parses your JS and HTML. JS never knows about PHP code.
Click CTRL+U to view source as browser sees it - there is no PHP code.
JS function is not run on page refresh. PHP code is run on page refresh.
First goes PHP parser:
session_start();
require("dbconfig.php");
$counter=0;
$counter++;
echo $counter;
Then goes JS/Html parser.
At this point your JS code looks like this:
function onclick() {
}
Content of function is empty because you output nothing from PHP side.
To fix it move PHP var to JS var:
<?php
session_start();
require("dbconfig.php");
$counter=0;
?>
<html>
<head>
<script>
var jsCounter = <?= $counter; ?>
function onclick() {
jsCounter++;
console.log(jsCounter);
}
</script>
</head>
<body>
link text
</body>
</html>
Note never output after closing body tag.
I'm using sessionStorage successfully in a project, with one caveat: I can't eliminate the storage with the clear() operator, as documented.
I'm doing this when logging out of the administrative mode of my site, which involves clicking on a Log Out item in a list, like this:
<li>Log Out</li>
The admin_logout.php file then destroys session variables, etc., and then redirects to the home page of the site. Its previous form, which works, is:
<?php
session_start();
session_destroy();
#header('Location:./');
exit;
?>
That all works fine. What I can't seem to integrate into the routine is clearing the sessionStorage. For the text of my admin_logout.php file, I've tried:
<?php
session_start();
?>
<script>
sessionStorage.clear();
</script>
<?php
session_destroy();
#header('Location:./');
exit;
?>
...as well as:
<?php
session_start();
echo '<script>';
echo 'sessionStorage.clear();';
echo '</script>';
session_destroy();
#header('Location:./');
exit;
?>
Perhaps pointing to the root cause is that when I've placed:
?>
<script>
alert("HELLO");
</script>
<?php
...within this script, the alert is never executed, yet everything else is. How can I invoke the <script> based sessionStorage.clear() operation to clear my session storage items within the routine listed above?
I think it's because you're redirecting on the server-side and the sessionStorage.clear() is happening on the client side. I believe you're redirecting before that gets a chance to run.
Allicam was correct; I needed to encapsulate the storage clearing code in a callback function:
<?php
session_start();
session_destroy();
<script type="text/javascript">
function firstFunction(_callback){
sessionStorage.clear();
_callback();
}
function secondFunction(){
firstFunction(function() {
window.location = './';
});
}
secondFunction();
</script>
I am trying to include some JavaScript to just one single page of a WordPress based website. Basically, what I've done is in the header.php of the theme, I've put the following:
<?php if( is_page('17')) { ?>
<!--Start of Zopim Live Chat Script-->
<script type="text/javascript">
window.$zopim||(function(d,s){var z=$zopim=function(c){z._.push(c)},$=z.s=
d.createElement(s),e=d.getElementsByTagName(s)[0];z.set=function(o){z.set.
_.push(o)};z._=[];z.set._=[];$.async=!0;$.setAttribute("charset","utf-8");
$.src="//v2.zopim.com/?2pL2gooCVnWNWjh0QB7IVqRgAiarsW4o";z.t=+new Date;$.
type="text/javascript";e.parentNode.insertBefore($,e)})(document,"script");
</script>
<!--End of Zopim Live Chat Script-->
<?php }
<?php endif; ?>
When I add this, it breaks the entire site, nothing loads anywhere. I've tried it with and without the
<?php endif; ?>
at the end, thinking that it may be duplicating with the
<?php }
I actually have two different conditional statements I need to add, each for a different page. I don't want to use the plugin that allows PHP & JavaScript in the pages themselves for security reasons, which is what I used to use.
Can anyone tell me what I'm doing wrong, and/or how to add this particular JavaScript to only page #17 (which I'm guessing will also show me how to add a PHP statement and link to single-use stylesheet that I need on a different page)?
It's hard to give a definite answer without seeing the rest of the page code but in my opinion you don't need the <?php endif; ?>.
Simply replace
<?php }
<?php endif; ?>
with
<?php } ?>
and it should work.
how could I use <?php echo $this->baseurl ?> or <?php echo $this->template ?> inside of an Javascript script?
Like this:
!window.jQuery && document.write(unescape('<script src="/xxx/<?php echo $this->baseurl ?>/templates/<?php echo $this->template ?>/js/jquery-1.7.2.min.js" data-mce-type="text/javascript" data-mce-type="text/javascript" data-mce-type="text/javascript" data-mce-type="text/javascript">
or in a script tag which is not in the index.php
<script type="text/javascript" src="/xxx/templates/<?php echo $this->template ?>/js/plugins.js"></script>
JavaScript fundamentally cannot execute any PHP code. Remember that PHP runs on the server, generates an HTML document, and sends it back to the browser. Then JavaScript begins running. This means whatever data you want to use in JavaScript must already be on the page by the time Joomla is finished running.
If you do need to fetch additional content from Joomla, look into using AJAX requests. You could build a page that outputs $this->template, for example, and then request that page from JavaScript in the background.
I have the following code to check whether the client has javascript enabled in their browser:
page.php:
<?php
session_start();
if(isset($_SESSION['gocheck'])) {$gocheck = $_SESSION['gocheck'];}
else {$gocheck = 'no';}
//echo $gocheck;
if($gocheck=='no'){header ("Location: ./gocheck.php"); exit;}
//Only reaches this line if gocheck.php has been run and Javascript is enabled.
unset($_SESSION['gocheck']);
//rest of page
?>
gocheck.php:
<?php
session_start();
$_SESSION['gocheck'] = 'yes';
echo"
<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org /TR/xhtml1/DTD/xhtml1-transitional.dtd\">
<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">
<head>
<script type=\"text/javascript\" language=\"JavaScript\">
window.location.replace('page.php');
</script>
</head>
<body>
This website requires Javascript to be enabled in your browser. <br />
Please enable Javascript and try again.
</body>
</html>
";
?>
So what should happen is the user is always redirected from page.php to gocheck.php, which sets the session variable $gocheck to 'yes' and directs back to page.php via Javascript. Because $gocheck is then equal to 'yes', page.php shouldn't direct back again tio gocheck.php.
This worked fine on my PC (using WAMP), but when I upload the files to the webhost, it seems to get stuck in an infinite redirect loop between page.php and gocheck.php. Also, if I echo $gocheck in page.php, it returns 'no', so it seems as if for some reason the session variable $gocheck is not being set properly by gocheck.php.
Could somebody please shed some light on this? Is there an error in my code? Is there something I need to change in php.ini on the webhost's server?
Thanks!
P.S. WAMP on my PC uses PHP v.5.3.0, but the webhost uses PHP v.5.2.12 - don't think this can be the problem though.
Why using php to detect if javascript is enabled or not? You can just add following html tags to your page:
<noscript>
Pleas enable javascript!
</noscript>
If the user then enables javascript and refreshes the page the javascript code will work.
ok never mind, now all session variables are being set properly, maybe it was just a question of waiting a bit until the webhost configured everything correctly (also had this problem with setting up e-mails)