Session data working local but not on remote server - php

this is my code. plain and simple.
1) first.html
<body>
<?php
session_start();
...
$somearray = $Object->method($somevar);
$_SESSION["somearray"] = $somearray;
...
?>
</body>
1) second.html
<body>
<div id="map_canvas">
<script language="javascript" type="text/javascript">
<?php session_start(); ?>
some_render_function(<?php echo json_encode($_SESSION["somearray"]); ?>);
</script>
</div>
</body>
perfectly working code on localhost.
There are 2 facts that can help you guys to come up with where's the problem here.
1)If you check the source of the page second.html offline and online you can respectively see some_render_function('all the stuff from the json') and some_render_function(NULL)
2)If i check my shared server folder i can see a directory called php_session with apparently all the correct files in it (of all the sessions opened when i tested my project, with CORRECT data in it)
Any hints?

Hello session_start ( http://php.net/manual/en/function.session-start.php ) should always be the first parameter on your page ...
Example
First Page
<?php session_start();?>
<html>
<head>
<title>First</title>
</head>
<body>
<?php
$somearray = $Object->method($somevar);
$_SESSION["somearray"] = $somearray;
?>
</body>
</html>
Second Page
<?php session_start(); ?>
<html>
<head>
<title>Second</title>
</head>
<body>
<?php
var_dump($_SESSION["somearray"]);
?>
</body>
</html>

Session_start should be before any output. So, move <?php to the start of file. Otherwise, behaviour depends on server configuration.

Related

Select php included html element with jQuery

I'd like to select an included html element with jQuery. How can I achive this?
index.php
<?php
<html>
<head>
...
</head>
<body>
include('file.php');
</body>
<html>
?>
file.php
<?php
echo '<div class="test">test</div>';
?>
select.js
let a = $('.test');
console.log(a); // returns undefined
Try executing your script after the page has fully loaded.
$(window).on("load", function() {
let test = $('.test');
console.log(test);
});
Code should be organized as follows: <?php Php.Code.Goes.Here ?>. This can be surrounded by HTML, for instance, <b><?php print("HELLO!"); ?></b>. This will display: HELLO!
So...you see the problem then, with...
<?php
<html>
<head>
...
</head>
<body>
include('file.php');
</body>
<html>
?>
The HTML is IN the PHP. You want this:
<html>
<head>
...
</head>
<body>
<?php include('file.php'); ?>
</body>
<html>
This code, of course, shouldn't have executed at all, since <?php <html> isn't really valid PHP syntax.

file_put_contents in head of another file using php

So, Im creating a library for other uses, but how can I make content from a file specificly go within the <head> or <body> html tag attribute etc...
for example, this is what im trying to make.
<html>
<head>
<?php include('content/starter/library.php'); ?>
<!-- From that included file, theres a script that put content in the head.-->
</head>
<body>
<!-- From that included file, theres a script that put content in the body -->
</body>
</html>
Im just trying to find another way instead of making multiple files for specific sections and do
<html>
<head>
<?php include('content/starter/library_head.php'); ?>
</head>
<body>
<?php include('content/starter/library_body.php'); ?>
</body>
</html>
Which I don't really want to do. Im not very good with javascript so, There no hope of me trying to figure out how to do this with javascript. Thanks for the answers in the future.
If you want to use one file (as your questions suggests) then one method is to create variables or functions in your library.php file and then echo them in your template
// contents of the library.php file...
<?php
$head_content = "put your <head> content here";
$body_content = "put your <body> content here";
?>
// your HTML file...
<?php include('content/starter/library.php'); ?>
<html>
<head>
<?php echo $head_content ?>
</head>
<body>
<?php echo $body_content ?>
</body>
</html>
UPDATE
To answer the question in your comment, here's an example using a function. You can put all of your code in a function and then just echo that anywhere in your document.
<?php
// contents of library.php...
function head() {
$return = '<link href="file.css" rel="stylesheet">';
$return .= '<link href="another_file.css" rel="stylesheet">';
return $return;
}
// your HTML file...
<html>
<head>
<?php echo head(); ?>
</head>
PHP functions explained: http://www.w3schools.com/php/php_functions.asp

passing php variables from dynamically created HTML Document

How to pass the php variables from dynamically created HTML page to next php file.
For Example. I have the following php code
<?php
session_start();
$uid=$_SESSION['uid'];
$doc=new DOMDocument('1.0');
$doc->loadHTML("
<html>
<head>
</head>
<body>
<a href='comments.php?id=`$uid`'> comments</a>
</body>
</html>
");
echo 'wrote:'. $doc->savedHTMLFile("/home/user/project1/test1.html"). 'bytes';
?>
Now when I see dynamically created HTML page, it just shows me the following code with .html extension; so how can I pass the php variable from this page to next file:
<html>
<head>
</head>
<body>
<a href='comments.php?id=`$uid`'> comments</a>
</body>
</html>
try to replace
<a href='comments.php?id=`$uid`'>
to
<a href='comments.php?id=$uid'>
try this... Your mistake is here 'comments.php?id=$uid' is string no php code use correct like this 'comments.php?id=".$uid."'.
<?php
session_start();
$uid = $_SESSION['uid'];
$doc = new DOMDocument('1.0');
$doc->loadHTML("
<html>
<head>
</head>
<body>
<a href='comments.php?id=".$uid."'> comments</a>
</body>
</html>
");
echo 'wrote:' . $doc->savedHTMLFile("/home/user/project1/test1.html") . 'bytes';
?>
Basically, you can use $_GET[''] method to do it. You just insert it into the URL, but I think, this will make the site vulnerable to SQL Injection.
remove back ticks(`) around $uid or use this code
<?php
session_start();
$uid=$_SESSION['uid'];
$doc=new DOMDocument('1.0');
$doc->loadHTML("
<html>
<head>
</head>
<body>
comments
</body>
</html>
");
echo 'wrote:'. $doc->savedHTMLFile("/home/user/project1/test1.html"). 'bytes';
?>

Inline javascript checks in PHP

I was trying to use <script> <noscript> checks inline with my PHP to render different form elements depending on weather or not someone has javascript enabled.
To display something to non enabled users only I can successfully use
<noscript> <?php ... ?> </noscript>
To display something to enabled users only I tried whats below but it does not work the same as above. Instead it allows all users to still see the PHP.
<script> <?php ... ?> </script>
How can I limit something to only those with javascript enabled without having two totally different pages?
You may be able to use something like this:
<script>
<?php
ob_start();
// ...
$scriptOnlyData = ob_get_clean();
?>
document.write(<?php echo json_encode($scriptOnlyData); ?>);
</script>
Javascript runs on the Client-side (in the browser), PHP runs on the Server-side (before the page is sent to the browser). So PHP does not know whether the browser does, or does not, have Javascript.
The simplest solution is to use Javascript itself to show/hide various elements within the browser, like so:
<!doctype html>
<html>
<head>
<title>Test Page</title>
<style>
.showWithJS {
display:none;
}
</style>
</head>
<body>
<div class="showWithJS">
This DIV, and anything with the class "showWithJS",
will only be seen when Javascript is enabled.
</div>
<div class="hideWithJS">
This DIV, and anything with the class "hideWithJS",
will not be seen when Javascript is enabled.
</div>
<script src="http://code.jquery.com/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('.showWithJS').show();
$('.hideWithJS').hide();
});
</script>
</body>
</html>
Another option would be to use Javascript to set a cookie (which can be read by PHP) to indicate whether that javascript is running. The downside of that, of course, is that, should the user switch javascript on/off mid-visit, the cookie will not update to reflect that change.
<?php
$javascriptOn = ( isset( $_COOKIE['javascript'] ) && $_COOKIE['javascript']=='on' );
?><!doctype html>
<html>
<head>
<title>Test Page</title>
</head>
<body>
<?php if( $javascriptOn ){ ?>
<div>
This DIV will only be seen when Javascript is enabled.
</div>
<?php } ?>
<?php if( !$javascriptOn ){ ?>
<div>
This DIV will not be seen when Javascript is enabled.
</div>
<?php } ?>
<script>
document.cookie = 'javascript=on';
</script>
</body>
</html>
The downside of this is that you need a pageload to set the cookie (it cannot be set and read by the same pageload), so you would need to refresh the page to see the changes.
Load the PHP in javascript and then insert when the DOM loads
JS:
function pageLoad(){
var text = "<?php ... ?>";
document.getElementById('scriptSpan').innerHTML = text;
}
html:
<span id="scriptSpan"></span>

php calling html

can you please let me know what I am missing:
<html>
<head><title>page</title></head>
<body>
<h2><center>Welcome to the mainpage</center></h2><br />
Home page
</body>
</html>
php code is :
<?PHP
$currpage=$_GET['currpage'];
echo "Hello world $currpage";
?>
when I click the homepage I want the sample4.php script to be executed and that output which is a html page to be displayed.
But when I click the homage page : I get a file window download.
I have the php script in the same location I have the html?
Does:
<html>
<head>
<title>My Page</title>
</head>
<body>
<p>Home page<p>
</body>
</html>
Do what you want? Otherwise, you'll need to show us the PHP code, in case it's making it a download from within the PHP.
For a hello world program, try this:
<html>
<head>
<title>My Page</title>
</head>
<body>
<?php
$currpage = isset($_REQUEST['currpage']) && is_int($_REQUEST['currpage'])?(int)$_REQUEST['currpage']:1;
echo "<p>Current page is $currpage</p>\n";
?>
</body>
</html>
You may also need to make sure that PHP is installed and running on your machine/server...
<html>
<head>
<title>My Page</title>
</head>
<body>
<?php
phpinfo();
?>
</body>
</html>

Categories