how do i put my output into a session - php

i have this code that receive form input example below code my problem is how do i put this output into session so even when i refress or reopen this page i we still have my output
code:
session_start();
if (isset($_POST["chedit"]))
{
$text = $_POST["text"];
$mumu = $_POST["edit"];
$_SESSION["text"] = $text;
$_SESSION["mumu"] = $mumu;
$text = $_SESSION["text"];
$mumu = $_SESSION["mumu"];
echo $text;
echo $mumu;
}
every thing is working fine but if i try to reopen this page i got my recent output lost can any fixed this? Big thanks

This way $_POST['text] and $_POST['edit'] will be stored in $_SESSION['text'] and $_SESSION['mumu'] and always printed:
<?php
session_start();
if (isset($_POST["chedit"]))
{
$text = $_POST["text"];
$mumu = $_POST["edit"];
$_SESSION["text"] = $text;
$_SESSION["mumu"] = $mumu;
}
if (isset($_SESSION["text"]) && isset($_SESSION["mumu"]))
{
echo '<p>' . $_SESSION["text"];
echo '<p>' . $_SESSION["mumu"];
}
?>
But of course you can use that variables however you want. They will be stored as long as you don't close the navigator.

Related

Using PHP to determine what HTML to write out

This block of PHP code prints out some information from a file in the directory, but I want the information printed out by echo to be used inside the HTML below it. Any help how to do this? Am I even asking this question right? Thanks.
if(array_pop($words) == "fulltrajectory.xyz") {
$DIR = explode("/",htmlspecialchars($_GET["name"]));
$truncatedDIR = array_pop($DIR);
$truncatedDIR2 = ''.implode("/",$DIR);
$conffile = fopen("/var/www/scmods/fileviewer/".$truncatedDIR2."/conf.txt",'r');
$line = trim(fgets($conffile));
while(!feof($conffile)) {
$words = preg_split('/\s+/',$line);
if(strcmp($words[0],"FROZENATOMS") == 0) {
print_r($words);
$frozen = implode(",", array_slice(preg_split('/\s+/',$line), 1));
}
$line = trim(fgets($conffile));
}
echo $frozen . "<br>";
}
?>
The above code prints out some information using an echo. The information printed out in that echo I want in the HTML code below where it has $PRINTHERE. How do I get it to do that? Thanks.
$("#btns").html(Jmol.jmolButton(jmolApplet0, "select atomno=[$PRINTHERE]; halos on;", "frozen on")
You just need to make sure that your file is a php file..
Then you can use html tags with php scripts, no need to add it using JS.
It's as simple as this:
<div>
<?php echo $PRINTHERE; ?>
</div>
Do remember that PHP is server-side and JS is client-side. But if you really want to do that, you can pass a php variable like this:
<script>
var print = <?php echo $PRINTHERE; ?>;
$("#btns").html(Jmol.jmolButton(jmolApplet0, "select atomno="+print+"; halos on;", "frozen on"));
</script>

PHP include HTML and echo out variable

I am working on a script with templates. So I have this PHP code:
<?php
$string = "TEST";
echo(file_get_contents('themes/default/test.html'));
?>
And I have this HTML (the test.html file):
<html>
<p>{$string}</p>
</html>
How can I make PHP actually display the variable inside the curly brackets? At the moment it displays {$string}.
P.S:
The string might also be an object with many many variables, and I will display them like that: {$object->variable}.
P.S 2: The HTML must stay as it is. This works:
$string = "I'm working!"
echo("The string is {$string}");
I need to use the same principle to display the value.
You can use the following code to achieve the desired result:
<?php
$string = "TEST";
$doc = file_get_contents('themes/default/test.html'));
echo preg_replace('/\{([A-Z]+)\}/', "$$1", $doc);
?>
P.S. Please note that it will assume that every string wrapped in { }
has a variable defined. So No error checking is implemented in the code above. furthermore it assumes that all variables have only alpha characters.
If it is possible to save your replacees in an array instead of normal variables you could use code below. I'm using it with a similar use case.
function loadFile($path) {
$vars = array();
$vars['string'] = "value";
$patterns = array_map("maskPattern", array_keys($vars));
$result = str_replace($patterns, $vars, file_get_contents($path));
return $result;
}
function maskPattern($value) {
return "{$" . $value . "}";
}
All you PHP must be in a <?php ?> block like this:
<html>
<p><?php echo "{" . $string . "}";?></p>
</html>
If you know the variable to replace in the html you can use the PHP function 'str_replace'. For your script,
$string = "TEST";
$content = file_get_contents('test.html');
$content = str_replace('{$string}', $string, $content);
echo($content);
It's simple to use echo.
<html>
<p>{<?php echo $string;?>}</p>
</html>
UPDATE 1:
After reading so many comments, found a solution, try this:
$string = "TEST";
$template = file_get_contents('themes/default/test.html', FILE_USE_INCLUDE_PATH);
$page = str_replace('{$string}',$string,$template);
echo $page;

PHP change directory according to parameter ($GET from url)

I try to run project and get localhost:8000/fishes.php and then change it to localhost:8000/fishes.php?fish=pike (or trout).
I have directories on same project /info/pike (or trout) and in these directories there is info.txt where first line has fish latin name and second line has average size.
My question is, How can I get text from that file to "site". Code doesn't include html-code, I don't have the code right now with me. But it is fine and runs normally.
Thanks
<?php
$species = $_GET["fish"];
if (file_exists("info.txt")) {
$array = explode("/n", file_get_contents('$species/info.txt'));
$name = $array[0];
$size = $array[1];
} else {
}
global $name;
global $size;
?>
<h1><?php$name?> (<?php$size?>)</h1>
In your Markup, you're opening the php tag, and calling a variable. This variable is not actually printing to the STDOUT. You have a few options:
<?php echo $name; ?>
or
<?php print $name; ?>
or if you have shorttags enabled
<?=$name;?>
You need to use echo or print for variables.
<?php
$name = '';
$size = '';
$species = $_GET["fish"];
if (file_exists("info.txt")) {
$array = explode("/n", file_get_contents('$species/info.txt'));
$name = $array[0];
$size = $array[1];
} else {
//Code for Else
}
//global $name; //No Need of Globals if you have html in same file
//global $size; //No Need of Globals if you have html in same file
?>
<h1><?php echo $name?> (<?php echo $size?>)</h1>
I think you need to use double quotes in "$species/info.txt"
if you server support short tags you can:
<h1><?=$name;?> (<?=$size;?>)</h1>

str_replace PHP script can't handle foreign characters such as umlauts robustly

The following script does not always correctly catch and convert foreign characters. Could someone show me what I'm missing to get it to be more robust?
<?php
include("../index_head.inc.php");
$content = implode("",(#file("current.txt")));
$url = "http://XXXXXX.html?no_body=1";
$content = file_get_contents($url,'r');
if (isset($_GET['showcurrent']) && $_GET['showcurrent'] == '')
{
$content = substr($content,1,strpos($content,"<hr ")-1);
}
else
{
$content = str_replace("<br style=\"clear:both\" />\n</p>", "</p>",$content);
$content = str_replace("ck1\"><img", "ck1\" target=_blank><img",$content);
};
$content = str_replace("<h3>current</h3>", "",$content);
echo "<div id=\"service\" style=\"width: 660px;padding-left:5px\">",str_replace("current.html","current.html",$content),"</div>";
include("../index_footer.inc.php");
?>
New information: Pekka, you gave me the idea to check how the page emits without str_replace():
<?php
include("../index_head.inc.php");
$content = implode("",(#file("current.txt")));
$url = "XXXXXX.html?no_body=1";
$content = file_get_contents($url,'r');
echo "<div id=\"service\" style=\"width: 660px;padding-left:5px\">",$content,"</div>";
It seems the problem lies elsewhere because I get the same mangling even without using str_replace()! If you can help me get this sorted out, I would sure appreciate it. I have seen your wish list. ;)
Did you include the charset in php?
try this:
header('Content-Type: text/html; charset=utf-8');
If not working check if your file is already saved in utf8 before str replace:
utf8_encode ( string $data );
In the opposite case use:
utf8_decode( string $data );
Hope it helps!
Thank you SBO - It sure did help! I simply changed the code to:
<?php
include("../index_head.inc.php");
$content = implode("",(#file("current.txt")));
$url = "http://XXXXXX.html?no_body=1";
$content = file_get_contents(utf8_encode($url),'r');
if (isset($_GET['showcurrent']) && $_GET['showcurrent'] == '')
{
$content = substr($content,1,strpos($content,"<hr ")-1);
}
else
{
$content = str_replace("<br style=\"clear:both\" />\n</p>", "</p>",$content);
$content = str_replace("ck1\"><img", "ck1\" target=_blank><img",$content);
};
$content = str_replace("<h3>current</h3>", "",$content);
echo "<div id=\"service\" style=\"width: 660px;padding-left:5px\">",str_replace("current.html","current.html",utf8_decode($content)),"</div>";
include("../index_footer.inc.php");
?>
and everything is working fine.
Thank you very much for your help.

Extract the text from webpage

In this test.php page i have this line of text
server=span.growler.ro&provider=-1&providersSerial=4&country=RO&mobile=0&token=eae5b2c50c123425d9351d8c8ee80b9a27ca3d69f15a669454b937eb
in this other test1.php?id=token page i have this php code runing
<?php
$Text=file_get_contents("./test.php");
if(isset($_GET["id"])){ $id = $_GET["id"];
$regex = "/".$id."=\'([^\']+)\'/";
preg_match_all($regex,$Text,$Match);
$fid=$Match[1][0];
echo $fid; } else { echo ""; } ?>
i need only the token
eae5b2c50c123425d9351d8c8ee80b9a27ca3d69f15a669454b937eb
to be show on test1.php?id=token
if in test.php the token looks like this
token='eae5b2c50c123425d9351d8c8ee80b9a27ca3d69f15a669454b937eb'
it works.
i needet to work from onother web page
$str = 'server=span.growler.ro&provider=-1&providersSerial=4&country=RO&mobile=0&token=eae5b2c50c123425d9351d8c8ee80b9a27ca3d69f15a669454b937eb';
parse_str($str, $vars);
$token = $vars['token'];
using with preg_match will help you .
$string ='server=span.growler.ro&provider=-1&providersSerial=4&country=RO&mobile=0&token=eae5b2c50c123425d9351d8c8ee80b9a27ca3d69f15a669454b937eb';
preg_match('/token=([a-f0-9]+)/i',$string,$matches);
echo $matches[1];
this will return you :
'eae5b2c50c123425d9351d8c8ee80b9a27ca3d69f15a669454b937eb'
I'd recommend you to use preg_match instead of preg_match_all
Try this regex:
$regex = "/&?token=([a-f0-9]*)&?/;

Categories