-1

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.

  • Possible duplicate of [Undefined variable: $\_SESSION](https://stackoverflow.com/questions/9650090/undefined-variable-session) – Philipp Jul 01 '18 at 11:18

1 Answers1

0
<?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');

Ambalan
  • 9
  • 2