I am making a web page where in my index.php file I run a JS function every so often and this function brings data from a php file called closeuser.php, in this case it is the session id.
Example: if I print the session id directly from the index file, it shows me the session id on the screen or with include and the file name if it shows me what the closeuser.php file has, as shown in the following code.
index.php file:
<?php
//session_start();
require_once 'Connections/swag.php';
$ses = session_id();
echo $ses;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/styles.css">
<link rel="shortcut icon" href="imgs/icon/favicon.ico">
<title>index</title>
<link href="font-awesome/css/all.min.css" rel="stylesheet">
</head>
<body>
<?php include("closeuser.php"); ?>
<script src="js/jquery-3.5.1.min.js"></script>
<script src="js/bootstrap.bundle.min.js"></script>
<script src="js/utiles.js"></script>
</body>
</html>
Closeuser.php file:
<?php
echo "a2";
$a = session_id();
echo $a;
?>
It prints me correctly, but if I do it with a js function, as in the following code, it doesn't show me anything, it shows me an empty string in the console, then I don't know why it's the problem:
$(function() {
cron(); // Lanzo cron la primera vez
function cron() {
$.ajax({
method: "POST",
url: "closeuser.php",
})
.done(function(msg) {
console.log(msg)
});
}
setInterval(function() {
cron();
}, 10000); // cada 10 segundos
});
Any idea why you send me the empty string? if I print another string in the closeuser.php file, for example echo "a1"; yes it shows me but the session id doesn't.
you need to add
#session_start();
in "closeuser.php"
Related
I have a page that needs a dynamically updated title. I have a .txt file that is dynamically updated by another script on my website, and I would like to set the content of that file as the page title (the <title> tag). Can somebody help me?
In PHP, you can read the txt file and make à echo later
<!DOCTYPE html>
<html lang="en">
<head>
<?php
$myfile = fopen("file.txt", "r") or die("Unable to open file!");
$content = fgets($myfile);
fclose($myfile);
?>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?php echo htmlspecialchars($content, ENT_QUOTES, 'UTF-8'); ?></title>
</head>
<body>
</body>
</html>
follow the below example, I am not tested it but try it once
<?php
$data = fopen("c:\\folder\\testing.txt", "r");
$ln_array = [];
$fp = fopen("testfile.txt", "r");
while (!feof($fp)) {
// do stuff to the current line here
array_push($ln_array, $fp);
}
fclose($fp);
?>
I'm new to PHP and am trying to create an Age Validation form that will limit access to specific content on a site if a user is under a certain age.
This is the HTML form (index.html):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<form action="data.php" method="POST">
<input type="date" name="date" />
<input type="submit" name="submit" />
</form>
</body>
</html>
and this is the PHP script(data.php):
<?php //Age Validation Form
session_start();
if (isset($_POST['submit'])) { //Check if button clicked on form page
$date2=date("Y-m-d");//today's date
$date1=new DateTime($_REQUEST['date']); //user date
$date2=new DateTime($date2);
$interval = $date1->diff($date2); //check diff between dates
$myage= $interval->y; //resulting age
if ($myage >= 16){ //full access to website is granted
$_SESSION[$limited] = false;
header('Location: ../index.php');
}else{ //limited access is granted
$_SESSION[$limited] = true;
header('Location: ../index.php');
}
}else{ //if result page page is loaded without form submission
echo "Return to start page";
}
?>
<form action="index.html"> <!--Return to form page-->
<button type="submit">Return</button>
</form>
I would like to be able and carry the resulting $limited variable from the PHP file into this HTML file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<script type="text/javascript">
function myFunction () {
var access = <?php echo $limited ?>;
var txt = document.createElement('h1');
txt.innerHTML = access;
document.body.appendChild(txt);
}
myFunction();
</script>
</body>
</html>
This is currently just for testing to make sure it can carry over. I have tried the $_SESSION method but i can't seem to figure it out.
Any and all possible solutions welcomed.
Thank you
First, your HTML file must be parsed by the PHP interpreter so it must have the .php extension in order to access any session variable.
Here's an example with 3 files in the same directory based on your question:
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<form action="data.php" method="POST">
<input type="date" name="date" />
<input type="submit" name="submit" />
</form>
</body>
</html>
data.php:
<?php //Age Validation Form
session_start();
if (isset($_POST['submit'])) { //Check if button clicked on form page
$date2=date("Y-m-d");//today's date
$date1=new DateTime($_REQUEST['date']); //user date
$date2=new DateTime($date2);
$interval = $date1->diff($date2); //check diff between dates
$myage= $interval->y; //resulting age
if ($myage >= 16){ //full access to website is granted
$_SESSION['limited'] = false;
header('Location: new.php');
}else{ //limited access is granted
$_SESSION['limited'] = true;
header('Location: new.php');
}
}else{ //if result page page is loaded without form submission
echo "Return to start page";
}
?>
<form action="index.html"> <!--Return to form page-->
<button type="submit">Return</button>
</form>
new.php(the new page, where you access your session variable)
<?php
session_start();
$limited = $_SESSION['limited'] ?? true;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<script type="text/javascript">
function myFunction () {
var access = <?php echo $limited ? "true" : "false" ?>;
var txt = document.createElement('h1');
txt.innerHTML = access;
document.body.appendChild(txt);
}
myFunction();
</script>
</body>
</html>
You're using $limited as a key in the $_SESSION array. What's in $limited ?
I'm pretty sure that if you reassign the correct value to $limited, you can access the value in $_SESSION with this:
<?php $limited = 'MyAwesomeKey'; ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<script type="text/javascript">
function myFunction () {
var access = <?php echo $_SESSION[$limited] ?>;
var txt = document.createElement('h1');
txt.innerHTML = access;
document.body.appendChild(txt);
}
myFunction();
</script>
</body>
</html>
I am trying to extract some JS generated data from a webpage, using PhantomJS.
I am able to get the page.content and I can see that the data I am interested in is enclosed within script and CDATA tags :
<!DOCTYPE html>
<html style="" class="someclass">
<head>
<meta class="meta-class-1">
<meta class="meta-class-1">
<link rel="shortcut" type="image/x-icon" href="/assets/...">
<meta content="width=device-width, initial-scale=1, maximum-scale=1.0" name="viewport">
<title>Page Title</title>
<link rel="stylesheet" media="all" href="/assets/page.css">
<script type="text/javascript" async="" src="https://www.google-analytics.com/analytics.js"></script>
<script>
//<![CDATA[
window.gon={};gon.data={ "Interesting data":"the data" };
//]]>
</script>
<script src="//anoterscript.js"></script>
</head>
<body>
</body>
</html>
Here is one of my unsuccessful attempts at getting one of the scripts' content :
"use strict";
var page = require('webpage').create();
page.open('https://prioridata.com/apps/monzo-1052238659/country-split', function () {
page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function() {
var scriptCtnt = page.evaluate(function() {
return [].map.call(document.getElementsByTagName('script')[0].innerHTML, function(data) {
return data;
});
});
console.log('Data is ' + JSON.stringify(data));
phantom.exit()
});
});
I've tried to parse the result in several different ways (libxml and node-phantom, page.content parsing using JQuery), but have been unable to get any script data so far.
Is it possible to achieve this using PhantomJs ? What am I doing wrong here ?
document.getElementsByTagName('script')[0].innerHTML
First look at the tag you are selecting:
<script type="text/javascript" async="" src="https://www.google-analytics.com/analytics.js"></script>
It doesn't have any content. If you want to get the script, you need to make a new HTTP request to https://www.google-analytics.com/analytics.js.
It looks like you actually want this script:
<script>
//<![CDATA[
window.gon={};gon.data={ "Interesting data":"the data" };
//]]>
</script>
That is not the first script on the page. You need to select the right tag.
Possibly just by using 1 instead of 0.
I have a input field which when selected opens a date range calendar.
I am trying to send the date values over AJAX to a PHP file to echo when the apply button is pressed. The AJAX post request does send, however when I try to test for detection in my PHP file nothing is returned.
I have searched online and have found nothing to indicate why. Here is my code
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script type="text/javascript" src="//cdn.jsdelivr.net/jquery/1/jquery.min.js"></script>
<script type="text/javascript" src="//cdn.jsdelivr.net/momentjs/latest/moment.min.js"></script>
<link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/bootstrap/latest/css/bootstrap.css" />
<!-- Include Date Range Picker -->
<script type="text/javascript" src="daterangepicker.js"></script>
<link rel="stylesheet" type="text/css" href="daterangepicker.css" /> </head>
<body>
<input type="text" name="datefilter" value="" id="dates" />
<script type="text/javascript">
$(function () {
$('input[name="datefilter"]').daterangepicker({
"showDropdowns": true
, "minDate": "01/01/2014"
});
$('input[name="datefilter"]').on('apply.daterangepicker', function (ev, picker) {
$(this).val(picker.startDate.format('MM/DD/YYYY') + ' - ' + picker.endDate.format('MM/DD/YYYY'));
});
$('input[name="datefilter"]').on('cancel.daterangepicker', function (ev, picker) {
$(this).val('');
});
$(".applyBtn").attr('id', 'testing');
$('#testing').click(function (e) {
$.ajax({
type: "POST",
url: 'test.php',
data: {
dates: $('#dates').val()
},
success: function (data) {
alert("success")
}
});
});
});
</script>
</body>
</html>
test.php
<?php
if (isset($_POST['dates'])) {
$range = $_POST['dates'];
echo $range;
} else {
echo "false";
}
?>
EDIT//
response body
The code works fine, the server response is in the "Response" tab:
instead of showing "success" in the alert, you could do this:
success: function (data) {
alert(data)
}
EDIT
Output in test.php:
put datefilter in a form
<form id="frm-test" action="test.php" method="post">
<input type="text" name="datefilter" value="" id="dates" />
</form>
dates -> datefilter in test.php
<?php
if (isset($_POST['datefilter'])) {
$range = $_POST['datefilter'];
echo $range;
} else {
echo "false";
}
I am looking to achieve page transition effects between 2 web domains. One being mcpixel.co.uk/new and the second being mcpaper.co.uk. I own both of the domains. I would like it so that if the link in the header is clicked, the page fade transitions to the new page, so there isn't any white or flicker between pages. Programs primarily programmed in PHP with Javascript/JQuery (Latest as of writing) and CSS.
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>
MCPaper - Your Number #1 Minecraft Newspaper
</title>
<meta name="description" content="A newspaper with news dedicated to Minecraft!">
<link rel="stylesheet" type="text/css" href="css/style.css" />
<link rel="stylesheet" type="text/css" href="css/fonts.css" />
<script type="text/javascript" src="css/jquery.js"></script>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#ip').click(function() {
$('.top-network').slideToggle("slow");
});
});
function toggle()
{
$('.top-network').slideToggle("slow");
}
</script>
</head>
<?php
if($_GET['ref']=='network')
{
echo "<body onload='toggle()'>";
echo "<center><div class='top-network' id='top-network'>";
echo "</div>";
}
else
{
echo "<body>";
echo '<center><div class="top-network" id="top-network" style="display: none;">';
echo "</div>";
}
?>
<div class="top">
<div class="top-container">
<div class="ip" id="ip">
<font color='yellow'>Click to see the RedstoneTor.ch Network</font>
</div>
This is the code I have, I haven't tried anything as I wouldn't know where to start.
On click hide the current page:
$('body').fadeOut('500');
Redirect to the new page:
window.location.href = "http://stackoverflow.com";
On the new page hide everything:
<body style="display:none;">
And script:
$(document).ready(function() {
$("body").fadeIn(2000);
});
But you might see a white page after fadeOut. It might be possible to make a better transition using an iframe...
You can do it simply with js/jquery:
On click event, fade out the page:
$('body').fadeOut('500');
Then redirect to the url you want.
Here is a tutorial: http://www.onextrapixel.com/2010/02/23/how-to-use-jquery-to-make-slick-page-transitions/