php session numeric variables not saved properly to other scripts - php

When assigning numeric values (latitude and longitude values) to $_SESSION variables the values are stored correctly on the original script. However, when a 2nd page accesses the $_SESSION variables the value is now the name of the variable. See below.
page1.php code
<?
php session_start(); ?>
<!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">
<head>
</head>
<body>
var latitude = 35.0;
var longitude = -89.0;
<?php
$_SESSION['wailat'] = latitude;
$_SESSION['wailng'] = longitude;
?>
<p>Latitude is ' + <?php echo $_SESSION['wailat']; ?> <br>Longitude is <?php echo $_SESSION['wailng']; ?></p>
Page 2
</body>
</html>
Page 1 output is correct:
Latitude is 35.0
Longitude is -89.0
Page 2 code:
<?php session_start(); ?>
<!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">
<head>
</head>
<body>
<?php
if (isset($_SESSION['wailat']) && isset($_SESSION['wailng']) ) {
echo 'Latitude ' . $_SESSION['wailat'] . '<br />Longitude ' . $_SESSION['wailng'];
} ?>
</body>
</html>
Page 2 output is incorrect:
Latitude is latitude
Longitude is longitude
Why do the two session variables' value on page 2 = the name of the variable that was assigned to them rather than the value?

Try this instead:
<?php
$latitude = 35;
$longitude = -89;
$_SESSION['wailat'] = $latitude;
$_SESSION['wailng'] = $longitude;
?>

You are writing the variables in html body , and it will display as it is on the page .this is not the way to declare variables. no variables are being created on the page. and even on the first page it will not echo the vars.
you must do it like #peter has mentioned.

Related

Accessing properties in an include when setting that include in to a file

I am trying to create a template html page which I will call via an include to set to a variable, this variable will then be used to set the value of a new file. I need the variables in the included file to be resolved so that the values are populated correctly.
To demo imagine these files:
main.php
$someVar = "someValue";
$fileText = include "aTemplate.php";
$newFileName = 'someFile.php';
if (file_put_contents($newFileName, $fileText) !== false) {
echo "File created (" . basename($newFileName) . ")";
} else {
echo "not created";
}
aTemplate.php
<?php
return
'<!doctype html>
<html lang="en">
<head>
<title><?php echo $someVar; ?></title>
</head>
</html>'
?>
What is currently happening is that the variables stay unresolved and hold no value so in the created html file the title is:
<title></title>
Instead of
<title>someValue</title>
How can I change the 'aTemplate.php' file to resolve the properties set in 'main.php'?
Just use this at your aTemplate.php:
<?php
return '<!doctype html>
<html lang="en">
<head>
<title>'. $someVar .'</title>
</head>
</html>';
?>
There are a couple of problems with your template, firstly as you have the HTML in single quotes, this won't do any of the string substitutions. Secondly, your trying to do a PHP echo whilst in HTML in PHP. I've used Heredoc to enclose the HTML as it allows any sorts of quotes and will also do the replacements.
The substitution of the value is just replaced by adding $someVar directly into the string.
So aTemplate.php becomes...
<?php
return <<< HTML
<!doctype html>
<html lang="en">
<head>
<title>$someVar</title>
</head>
</html>
HTML;
You should echo those string in page instead of return command.
The keyword return is used inside a function while your file is not a function. The browser simply puts what's inside include file has to offer. In you case it is HTML string which should be outputted using echo command.
Also the server executes code in top to bottom and left to right. Thus the variable $someVar will be accessed in aTemplate.php file.
Use below code instead to work
main.php
$someVar = "someValue";
$file = 'aTemplate.php';
// Open the file to get existing content
$fileText = include "aTemplate.php";
$newFileName = 'someFile.php';
// Write the contents back to the new file
if (file_put_contents($newFileName, $fileText) !== false)
{
echo "File created (" . basename($newFileName) . ")"; }
else {
echo "not created";
}
aTemplate.php
<!doctype html> <html lang="en"><head>
<title><?php echo $someVar;?></title>
</head>
</html>

Ping server every 1 second infinitely till closed?

I'm wanting to set up a loop or maybe a page refresh that pings my server over and over again and tells me the milliseconds.
This is the code I'm using but not sure how to make it keep refreshing and giving me the response live. Can someone show me how to make it live so it constantly updates every 1 second or even every 2 or 3 seconds is fine also. Just need it to be live.
<?php
function pingDomain($domain){
$starttime = microtime(true);
// supress error messages with #
$file = #fsockopen($domain, 80, $errno, $errstr, 10);
$stoptime = microtime(true);
$status = 0;
if (!$file){
$status = -1; // Site is down
}
else{
fclose($file);
$status = ($stoptime - $starttime) * 1000;
$status = floor($status);
}
return $status;
}
?>
Server Latency: <?php echo pingDomain('192.168.1.20'); ?> ms<br>
Edited with Paul's Code still no luck:
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html;" />
<meta http-equiv="refresh" content="1; url='yourPage.php'" />
</head>
<body>
<?php
function pingDomain($domain){
$starttime = microtime(true);
// supress error messages with #
$file = #fsockopen($domain, 80, $errno, $errstr, 10);
$stoptime = microtime(true);
$status = 0;
if (!$file){
$status = -1; // Site is down
}
else{
fclose($file);
$status = ($stoptime - $starttime) * 1000;
$status = floor($status);
}
return $status;
}
?>
Server Latency: <?php echo pingDomain('192.168.1.20'); ?> ms<br>
</body>
</html>
There are two ways I can think of:
1.Setting up refresh attribute in the page header
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html;" />
<meta http-equiv="refresh" content="1; url='yourPage.php'" />
</head>
<body>
<?php
//your php code here
?>
</body>
</html>
2.Use a crontab job to execute this command every second:
w3m http://yourhost/yourPage.php
I think the first solution is closer to what you need.

Logging IP, Browser, Operating System...etc

I'm trying to create a page what will log a plethora of user data for security purposes but am am unsure how best to do so.
Ideally I want all important stats for example their IP, their X-Forward IP, their browser, operating system and any other details I can get.
I've also got problems showing the variables in a php / html page as shown below:
<?php
$ip = <?= $_SERVER['REMOTE_ADDR'];
$template = '<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Title</title>
</head>
<body>
IP: <?= $ip ?>
IP: [IP]
</body>
</html>';
$template = str_replace('[IP]', $ip, $template);
echo $template;
?>
or
<?php
$ip = <?= $_SERVER['REMOTE_ADDR'];
echo $ip;
?>
Try something simple first:
<?php
$ip = $_SERVER['REMOTE_ADDR'];
echo $ip;
?>
For a full list of SERVER superglobal variables, check this link http://php.net/manual/en/reserved.variables.server.php
<?php
$data = $_SERVER;
$IP = $data['REMOTE_ADDR'];
echo "IP address is:" . $IP;
?>
There you go... now look up all the available $_SERVER values and use the ones you need.

Passing html form values to php file

Whether I enter the value for bug id or not ..in both conditions the code between php tags is displayed as output. Can someone help me to find out the reason.
Code is given below:
html file-------------------------------------------------------------
<!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Bug Report</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>
<body>
<h2>Bug Report</h2>
<form action="test.php" method="post" >
<p>Bug ID:<input type="text" name="bugid" size="20" /></p>
<p><input type="submit" value="Record Bug" /></p>
</form>
</body>
</html>
php file--------------------------------------------------
<!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Record Bug</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>
<body>
<?php
$bugid=$_POST["bugid"];
echo $bugid;
if (empty($bugid))
{
echo "<p>You must enter the Bug ID to record the bug.</p>";
}
else
{
echo"<p>good</p>";
}
?>
</body>
</html>
If you're getting PHP code in the output, then your webserver isn't running that page/script through the PHP interpreter. Generally, that's because you've put the code into a .html file, which is not treated as PHP by default.
Either rename the file to whatever.php, or reconfigure your webserver to treat .html files as PHP scripts.
check that php is working or not for that write the code <?php phpinfo(); ?> and if have manually installed php apache and getting problem try wamp server
your code is widely open for sql-injunction to make it secure use
public function mysql_prep( $value ) {
$magic_quotes_active = get_magic_quotes_gpc();
$new_enough_php = function_exists( "mysql_real_escape_string" ); // i.e. PHP >= v4.3.0
if( $new_enough_php ) { // PHP v4.3.0 or higher
// undo any magic quote effects so mysql_real_escape_string can do the work
if( $magic_quotes_active ) { $value = stripslashes( $value ); }
$value = mysql_real_escape_string( $value );
} else { // before PHP v4.3.0
// if magic quotes aren't already on then add slashes manually
if( !$magic_quotes_active ) { $value = addslashes( $value ); }
// if magic quotes are active, then the slashes already exist
}
return $value;
}
Check whether php is running or not in your machine. Save the below code as test.php and run it through
<?php
phpinfo();
?>
In that case you have to run on that Server which support PHP like Xampp or Wamp and also extension of the file should be .php

PHP file_put_contents new line

I tried use file_put_contents output new page. but I meet some trouble in breaking new line.
<?php
$data ='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\r\n';
$data .='<html xmlns="http://www.w3.org/1999/xhtml" lang="en">\r\n';
$data .='<head>\r\n';
$data .='</head>\r\n';
$data .='<body>\r\n';
$data .='<p>put something here</p>\r\n';
$data .='</body>\r\n';
$data .='</html>\r\n';
file_put_contents( dirname(__FILE__) . '/new.php', $data);
?>
I tried \n or \r\n, they all can not make a new line:
1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\r\n<html xmlns="http://www.w3.org/1999/xhtml" lang="en">\r\n<head>\r\n</head>\r\n<body>\r\n<p>put something here</p>\r\n</body>\r\n</html>\r\n
Using \r or \n in single quotes carries it literally. use double quotes instead like "\r\n"
So one line might become:
$data .= "<head>\r\n";
or
$data .='<head>' . "\r\n";
You are using single-quoted character literals, which don't interpret escape sequences.
Either switch to double-quoted strings or, preferably, use heredoc syntax.
<?php
$data = <<<CONTENTS
<!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" lang="en">
<head>
</head>
<body>
<p>put something here</p>
</body>
</html>
CONTENTS;
file_put_contents( dirname(__FILE__) . '/new.php', $data);
?>
But really, why are you writing a hard-coded file? That's really strange.

Categories