When I try to open js.php, it gives an error in xampp while in 000webhost it doesn't . I don't understand why.
so here is the code.
js.php
<?php
header('Content: text/javascript');
if($_SESSION['myjskey'] != hash('md5','examplekey')){
die('No Auth');
}
$_SESSION['JSSESSID'] = 'change';//so that no one can access directly
?>
//secret js
alert('javascript done.');
//and some more js
index.php
<?php
session_start();
$_SESSION['myjskey'] = hash('md5','examplekey');
?>
<!DOCTYPE html>
<html>
<head>
<title>PHP</title>
</head>
<body>
this is a test.
<script type="text/javascript"><?php include('js.php'); ?></script>
</body>
</html>
Website js.php
Xampp Js.php
Edit 1: I cannot do session_start(); in js.php as it will give an error index.php
(session already started.) Removing the header didn't work.
<?php
//js
if($_SESSION['myjskey'] != hash('md5','examplekey')){
die('No Auth');
}
$_SESSION['JSSESSID'] = 'change';//so that no one can access directly
?>
//secret js
alert('javascript done.');
//and some more js
make this change no need to use
header('Content: text/javascript');
Related
somehow I couldn't find an answer to the following problem when searching the web.
I'm not familiar with PHP and am trying to get the below PHP code, which I put right at the beginning of the file, to display the HTML page that follows, instead of "Welcome.".
Many thanks for your time!
<?php
ob_start();
session_start();
if(!isset($_SESSION['userid'])) {
die('<script>window.location = "https://test.com/login.php"</script>');
}
$userid = $_SESSION['userid'];
echo "Welcome.";
?>
<!DOCTYPE html>
<html lang="De">
<head>
<meta charset="UTF-8"/>
...
I found the solution...
<?php
ob_start();
session_start();
if(!isset($_SESSION['userid'])) {
die('<script>window.location = "https://test.com/login.php"</script>');
}
$userid = $_SESSION['userid'];
echo <<<HTML
--> HTML section/code <--
HTML;
?>
I'm trying to pass the php variable $shareURL = "someURL"; from the parent page of test.php into the included file of commentTest.php. Is this possible? If yes, please help.
Parent File = Test.php
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
<?php
$shareURL = "someURL";
echo "$shareURL";
include "http://domainName.net/assets/includes/commentTest.php";
?>
</body>
</html>
PHP Included File = commentTest.php
<?PHP
echo "<div class='fb-comments' data-href='$shareURL' data-num-posts='5' data-width='100%'></div>";
?>
HTML Output Source
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
someURL<div class='fb-comments' data-href='' data-num-posts='5' data-width='100%'></div></body>
</html>
Change your Test.php to this:
include "/assets/includes/commentTest.php";
Thanks everyone!
Your comments and answers helped me find the solution I needed.
$root = dirname(__FILE__);
include "$root/assets/includes/commentTest.php";
Apparently my root is here /var/www/html instead of right after the TLD in the URL.
I need to display html source code form other php file.
I have two file
code.php
index.php (I hope I can convert the code.php to html source code.)
code.php:
<!DOCTYPE html> <html> <body> <?php $color = "red"; echo $color; ?> </body> </html>
index.php (I hope I can convert the code.php to html source code.)
$php_to_html = file_get_contents("code.php");
$html_encoded = htmlentities($php_to_html);
echo $html_encoded;
but when i run the index.php file, the result is
<!DOCTYPE html> <html> <body> <?php $color = "red"; echo $color; ?> </body> </html>
but I hope I can see the result is
<!DOCTYPE html> <html> <body> red </body> </html>
any idea how can i do this ,thanks!!!
You want to execute the PHP, so include it and capture the output:
ob_start();
include("code.php");
$php_to_html = ob_get_clean();
$html_encoded = htmlentities($php_to_html);
echo $html_encoded;
If you want the HTML to be rendered as HTML then don't use htmlentities().
Optionally (not the best way) but you can execute it by retrieving from the URL:
$php_to_html = file_get_contents("http://www.example.com/code.php");
$html_encoded = htmlentities($php_to_html);
echo $html_encoded;
Buffer output and include it:
ob_start();
include_once('code.php');
$html = ob_get_clean();
By using output buffering, any output is not sent to the browser, but instead kept in memory. This allows you to run the code, and get the output as a variable. ob_get_clean() flushes the buffer (in this case into our $html variable), and then stops buffering, allowing you to continue as normal. :-)
Following is the php code that I was using. I am trying to run this script(residing in the same directory as the php file is in) and want to display the output of the script on webpage. Script is working fine through command prompt, but not working thru php script.
<html>
<head>
<title>py script</title>
</head>
<body>
<h1>hey there!</h1>
<?
$pyscript = 'C:\\xampp_new\\htdocs\\projectx\\USR.py';
$python = 'C:\\Python27\\python.exe';
exec("$python $pyscript ", $output, $return );
echo $return;
?>
</body>
</html>
<html>
<head>
<title>py script</title>
</head>
<body>
<h1>hey there!</h1>
<?
$pyscript = 'C:/xampp_new/htdocs/projectx/USR.py';
$python = 'C:/Python27/python.exe';
$command=escapeshellcmd('C:/xampp_new/htdocs/projects/USR.py');
$output=shell_exec($command);
echo $output;
?>
</body>
</html>
There are several options why your exec call won't work:
Are you running om safe_mode ? exec is disabled in safe mode
Your std output is at $output which is more interesting than the return value
if your python script working ? try in PHP: exec("$python $pyscript >test.txt"); and see if your text file has anything in it
I have already search about this question,I want to remove line break from the html,but there a some split php line in the html file,for example:
<html>
<head>
<title><?='abc'?></title>
</head>
<?php
if($_SESSION['test'] == 'yes'){
echo 'hello';
}
?>
123456
43567
<?='13245tryt57u68'?>
</body>
</html>
how can I remove line break from this php file?
You could use ob_start();
<?php ob_start();
//Start page output
?>
<html>
<head>
<title><?='abc';?></title>
</head>
<body>
<?php
if(isset($_SESSION['test']) && $_SESSION['test'] == 'yes'){
echo 'hello';
}
?>
123456
43567
<?='13245tryt57u68';?>
</body>
</html>
<?php
//End page output and assign the contents to a variable
$buffer = ob_get_contents();
ob_end_clean();
//Replace the new line with null
echo str_replace("\n",null,$buffer);
?>
Result:
<html><head><title>abc</title></head><body>1234564356713245tryt57u68</body></html>
If you want it in your IDE:
Use replace function, and replace \n by no character
If you want it for the client:
You have to configure your htaccess to remove the lines breaks/indenting when sending the file to the client, ie by using the mod_pagespeed extension:
http://www.the-art-of-web.com/system/mod-pagespeed-settings/ and the collapse_whitespace option