Using sessions to transport a variable over multiple pages - php

I want to make a transition page, to forward "URL" to. I have this code. Self explanatory. !var means that some var is not given. URL is some url like http://domain.com
session_start();
if (!$some_variable) {
$data = "http://localhost/";
$_SESSION['keks'] = $data;
require("transition.php");
exit();
}
transition.php:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN"
"http://www.w3.org/TR/html4/frameset.dtd">
<html>
<head>
<title>Frameset</title>
</head>
<frameset rows="50%,50%">
<frame src="above.php" name="Navigation">
<frame src="http://www.domain.com" name="Daten">
<noframes>
<body>
<p>Something</p>
</body>
</noframes>
</frameset>
</html>
above.php:
<html>
<head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
<title>
</title>
</head>
<body>
<div style="text-align: right;">Continue
</div>
</body>
</html>
Which leads to
<a href="">
Why is that so?

It's always best to give full code and not use !var - just put in the correct variable here as it is less confusing. Secondly you don't have any session in above.php
Unknown file:
<?php
session_start();
if (!isset($_SESSION['keks'])) {
//header("Location: ".URL, true, 301);
$_SESSION['keks'] = "http://localhost/";
require("transition.php");
exit();
}
?>
above.php
<?php
session_start();
?>
<html>
<head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
<title></title>
</head>
<body>
<div style="text-align: right;">
Continue
</div>
</body>
</html>

This should work:
session_start();
if (!isset($_SESSION['keks'])) {
$data = 'URL';
$_SESSION['keks'] = $data;
require("transition.php");
exit();
}
Also in above.php you need
<?php session_start(); ?>
To get the variable.

Related

Why is PHP $_SESSION not working on other pages?

I was trying the "$_SESSION" function, but it didn't work. Here are the codes.
test1.php
<?php
session_start();
$_SESSION['testid']= "12";
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Test Page</title>
</head>
<body>
<h1><?php echo $_SESSION['testid']; ?></h1>
</body>
</html>
The output was:
12
test2.php
<?php
session_start();
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Test Page 2</title>
</head>
<body>
<h1><?php echo $_SESSION['testid']; ?></h1>
</body>
</html>
The output was nothing.
So, what is the problem?

Use PHP string at start of page, that were setted at the end of page

Is there any way to get PHP string value before it was setted. Maybe it's possible to make with substr_replace?
I need to write this code:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title><?php echo $monkey;></title>
</head>
<body>
<?php
$monkey = "I like banana!";
echo $monkey;
?>
</body>
</html>
To get this result:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>I like banana!</title>
</head>
<body>
I like banana!
</body>
</html>
I know I could set $monkey before <html> code, but can I declare $monkey at the end of page to get it at the beginning?
UPDATE:
This example not working:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title><?php title();?></title> <!-- Need result I like banana! -->
</head>
<body>
<?php
$monkey = "I like banana!";
function title(){
echo "$monkey";
}
title();
?>
</body>
</html>
Here ya go! Just use a function!
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title><?php title();?></title>
</head>
<body>
<?php
function title(){
$monkey = "I like banana!";
echo $monkey;
}
title();
?>
</body>
</html>
Why not just set the variable before you start emitting the page?
<?php $monkey = "I like bananas"; ?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title><?php echo $monkey;?></title>
</head>
<body>
<?php
echo $monkey;
?>
</body>
</html>

How can I show php variable details inside an html form which is inside an echo php?

I have a form in which some I have defined some variables in PHP, The question is I need these variables details inside an html which is inside an echo . I don't know what to do as I have tried it.
<?php $var= "LOVE" ?>
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<?php echo "<!DOCTYPE html>
<html>
<head>
<title>Details</title>
</head>
<body>
<div > <h1> echo $var ;</h1></div>
</body>
</html>";?>
</body>
</html>
You just need to write the variable on double quote. Here an example
<?php
echo "<html>$var</html>"
// OR if one quote
echo '<html>'.$var.'</html>';
?>
<?php $var= "LOVE"; ?>
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<?php echo "
<!DOCTYPE html>
<html>
<head>
<title>Details</title>
</head>
<body>
<div >
<h1>".$var."</h1>
</div>
</body>
</html>";?>
</body>
</html>
There is no need of adding <html>,<body> tags inside another. There is already one above in your code. I do not know why you have used this pattern.

How to remove whitespaces from generated HTML?

FUNCTIONS.PHP
<?php
function global_header($page)
{
echo "
<!doctype html>
<html lang='en'>
<head>
<meta charset='utf-8' />
<title>" . $page . "</title>
<meta name='description' content='BTI320 Assignment 2' />
</head>
<body>
";
}
?>
<?php
function global_footer()
{
echo "
</body>
</html>
";
}
?>
When I view my page source in chrome/FF I get the following source:
<!doctype html>
<html lang='en'>
<head>
<meta charset='utf-8' />
<title>Add</title>
<meta name='description' content='BTI320 Assignment 2' />
</head>
<body>
</body>
</html>
It's indented by about 3 tabs. Is there a PHP strip function or something that can align it properly? I don't like my entire pages HTML being messed up.
My expected output is to not be indented.
The reason you are getting indented outputs is that you are echoing them like that...
Simply remove the indentaions from the echo statements to get rid of them
<?php
function global_header($page)
{
echo "
<!doctype html>
<html lang='en'>
<head>
<meta charset='utf-8' />
<title>" . $page . "</title>
<meta name='description' content='BTI320 Assignment 2' />
</head>
<body>";
}
?>
<?php
function global_footer()
{
echo "
</body>
</html>";
}
?>
This makes your php harder to follow fut the output will be as you requested
Consider using a template engine. Direct output of HTML strings is considered bad practice.
If you don't want to use third-party template engines, you can anyway benefit from some simplified templating like this:
page.tpl template file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset='utf-8' />
<title>{{title}}</title>
</head>
<body>
{{body}}
</body>
</html>
PHP:
// Loading HTML code that does not contain any undesired whitespace.
$code = file_get_contents('page.tpl');
// Replacing template variables with their values.
$code = str_replace(
array(
'{{title}}',
'{{body}}'
),
array(
'Example title',
'Page body'
),
$code
);
// Outputting resulting HTML code.
echo $code;

Header - Forwarding variables via PHP to pages without $_GET

I have one page A having this code snippet
if (!var) {
header("Location: ".URL, true, 301);
exit();
}
if (!var2) {
header("Location: ". $url2, true, 301);
exit();
}
I want to create a page in between URL/$url2 and A I call this one B.
How do I have to change A to give B the content of URL/$url2 without the user seeing it? I could use something like
Location: mypage.php?url=$url2
But the user could change that what I don't want. If you recommend $_POST how would you do it? If not, what would you do?
I changed it to
session_start();
if (!var) {
//header("Location: ".URL, true, 301);
$data = URL;
$_SESSION['keks'] = $data;
require("transition.php");
exit();
}
transition.php:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN"
"http://www.w3.org/TR/html4/frameset.dtd">
<html>
<head>
<title>Frameset</title>
</head>
<frameset rows="50%,50%">
<frame src="above.php" name="Navigation">
<frame src="http://www.domain.com" name="Daten">
<noframes>
<body>
<p>Something</p>
</body>
</noframes>
</frameset>
</html>
above.php:
<html>
<head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
<title>
</title>
</head>
<body>
<div style="text-align: right;">Continue
</div>
</body>
</html>
Which leads to
<a href="">
Use a session cookie.
On the page with the info you can save text etc into a session var like this:
$data = 'hello';
$_SESSION['xxx'] = $data;
And get it back on the next page like this:
echo $_SESSION['xxx'];
// Hello
Dont forget you need to run session_start(); before using sessions.

Categories