I had a problem that I was struggling with a couple days ago, and the problem lies in my session id handling while using localhost (xampp local server).
What I now tried to do, is to create two separate pages, the first one randomizes a session id in such a way:
<?php
session_start();
$random_number = rand();
$_SESSION['random_number'] = $random_number;
?>
<!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" xml:lang="en"
lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=utf-8"/>
<link rel="stylesheet" href="style.css" />
<title>Understanding PHP sessions...Page 1</title>
</head>
<body>
<div id="contentarea">
<div id="innercontentarea">
<h2>Understanding PHP sessions...Page 1</h2>
<p>Random number generated
<span style="font-weight:bold;">
<?php echo $_SESSION['random_number']; ?>
</span>
</p>
<p>PHP session id
<span style="text-decoration:underline;">
<?php echo session_id(); ?>
</span>
</p>
Go to next page
</div>
</div>
</body>
</html>
So, this creates a random session number here, and should output the same number on the second page basic_session2, which basically the same, but the difference is in session initialization, which is only:
<?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" xml:lang="en"
lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=utf-8"/>
<link rel="stylesheet" href="style.css" />
<title>Understanding PHP sessions...Page 2</title>
</head>
<body>
<div id="contentarea">
<div id="innercontentarea">
<h2>Understanding PHP sessions...Page 2</h2>
<p>My Favorite movie is
<span style="font-weight:bold;">
<?php echo $_SESSION['random_number']; ?>
</span>
</p>
<p>PHP session id
<span style="text-decoration:underline;">
<?php echo session_id(); ?>
</span>
</p>
</div>
</div>
</body>
</html>
But it echoes out a different random number! So, now, I am guessing there is a problem with my session cookie path, as this session number is not holding up.
The system I use is Ubuntu, and the localhost directory for xampp is a standard one. Do you know how to solve that issue, or maybe encountered such a problem?
Related
So the idea was this, if i use example.php?u=8, UTF-8 will be used.
I thought this would do the trick.
<?php
$u=($_GET["u"]); // 8 is utf8
?>
<!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 name="viewport" content="width=device-width, initial-scale=1">
<?php if ($u=8): ?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php else: ?>
<?php endif ?>
<title>just any title</title>
</head>
<body>
</body>
</html>
Obviously I am doing something completely, wrong, since it doesnt even give me the variable.
Where did i go wrong?
Look, i have html with included php link but that doesn't work well.Information what was in html doesn't come to php page. Please help with that.
HTML CODE:
<!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" xml:lang="en"
➝ lang="en">
<head>
<meta http-equiv="Content-Type"
➝ content="text/html;
➝ charset=utf-8"/>
<title>Greetings!</title>
</head>
<body>
<!-- Script 3.6 - hello.html -->
<div><p>Click a link to say
➝ hello:</p>
<ul>
<li><a href="hello.php?
name=Michael">Michael</a></li>
<li><a href="hello.php?
name=Celia">Celia</a></li>
<li><a href="hello.php?
name=Jude">Jude</a></li>
<li><a href="hello.php?
name=Sophie">Sophie</a></li>
</ul>
</div>
</body>
</html>
PHP code:
<!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" xml:lang="en"
➝ lang="en">
<head>
<meta http-equiv="Content-Type"
➝ content="text/html;
➝ charset=utf-8"/>
<title>Greetings!</title>
</head>
<body>
<?php // Script 3.7 - hello.php
error_reporting (E_ALL | E_STRICT);
$name - $_GET['name'];
print "<p>Hello, <span
style=\font-weight:
bold;\">$name</span>!</p>";
?>
</body>
</html>
And this how it's look on web
http://santa-monica.comli.com/hello.html
http://santa-monica.comli.com/hello.php?name=Michael
change your php code to this -
<!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" xml:lang="en"
➝ lang="en">
<head>
<meta http-equiv="Content-Type"
➝ content="text/html;
➝ charset=utf-8"/>
<title>Greetings!</title>
</head>
<body>
<?php // Script 3.7 - hello.php
error_reporting (E_ALL | E_STRICT);
$name = $_GET['name'];
print '<p>Hello, <span style="font-weight:bold;">'.$name.'</span>!</p>';
?>
</body>
</html>
your link to the php page is working fine, your problem is where u define the name variable, you have a typo, instead of $name = $_GET['name']; you put $name - $_GET['name'];, dont worry this happens to the best of us, fix that and it should run smoothly, btw if you are testing your php code on an online server dont, because it is slower than on your pc, unless you are contacting an api, it doesnt work unless its executing online
EDIT :
change your code to this :
<?php // Script 3.7 - hello.php
error_reporting (E_ALL | E_STRICT);
$name = $_GET['name'];
?>
<p>Hello, <span
style=\font-weight:
bold;\"><?php echo $name; ?></span>!</p>
So I have a header file:
<html>
<head>
<link href="/design_header.css" rel="stylesheet" type="text/css" />
</head>
<body>
content
</body>
</html>
And I want to place this header in my file
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
meta tags etc.
</head>
<body>
<div id="container_big">
<?php include 'header_login.php'; ?>
content
</div>
</body>
</html>
The problem is now I have regular <html>, <head>, <body> tags inside the <body> tag of my webpage. In the html validator I receive errors like
Stray start tag html.
Stray end tag head.
An body start tag seen but an element of th
e same type was already open.
How to do it properly? Btw. I also place a footer to the bottom of the webpage the same way.
Your header file should only contain the HTML text you want for the header.
As it will be inserted into another webpage, it should not be a full HTML document.
Having only HTML for Header in Header
One option is to instead include in your header file only the HTML that is for the header (used by all pages that include it). However this has the downside that your not following the recommendation to have CSS loading before is rendered.
See: https://stackoverflow.com/a/1642259/1688441
Header File
<link href="/design_header.css" rel="stylesheet" type="text/css" />
content
Other files
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
meta tags etc.
</head>
<body>
<div id="container_big">
<?php include 'header_login.php'; ?>
content
</div>
</body>
</html>
Having only HTML for Header in HeaderFile and Separate HeadFile
You could have have a separate global_head_include.html (or txt, php, etc) file and put your CSS code there.
Header File (for include)
Header content
File with CSS includes (for include)
<link href="/design_header.css" rel="stylesheet" type="text/css" />
Whatever else is global...
Other files
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
meta tags etc.
<?php include 'global_head_include.php'; ?>
</head>
<body>
<div id="container_big">
<?php include 'header_login.php'; ?>
content
</div>
</body>
</html>
This is how I set out my headers and footers in my current PHP site:
header.php:
<!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>James Wright - Young Software Developer</title>
<link rel="stylesheet" href="style.css" />
<meta name="description" content="The online home of a young web designer and software developer." />
<link rel="icon" type="image/png" href="/img/ico.png" />
</head>
<body>
<div id="holder">
<div id="menu"></div>
<div id="mainContent">
footer.php:
</div>
<div id="mainReflect"></div>
<p class="footer">© James Wright <?php echo date("Y"); ?>. Minimum resolution: 1024x768 <a href="http://validator.w3.org/check?uri=referer" target="_blank"><img
src="http://www.w3.org/Icons/valid-xhtml10" alt="Valid XHTML 1.0 Transitional" height="31" width="88" style="vertical-align: middle;"/></a>
<a href='http://www.powermapper.com/products/sortsite/'><img src='http://www.powermapper.com/images/badge-v1/sortsite-badge-small-5.png' width="80" height="15" alt='Broken link checker and accessibility checker top 5% - SortSite' style='vertical-align: middle;'/></a>
</p>
</div>
</body>
</html>
Then whenever I create a new page, all I need to do is this:
<?php include("header.php"); ?>
<p>Main body content!</p>
<?php include("footer.php"); ?>
I currently am trying to use print inside of the JQuery footer by writing it is not working. Instead nothing shows up. In the earlier php page I stored it into the session using...
session_start();
$_SESSION['username'] = $_POST['username'];
my code in my html page is as follows...
<?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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Food For Thought</title>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0a1/jquery.mobile-1.0a1.min.css" />
<script src="http://code.jquery.com/jquery-1.4.3.min.js"></script>
<script src="http://code.jquery.com/mobile/1.0a1/jquery.mobile-1.0a1.min.js"></script>
</head>
<body>
<div data-role="page" id="page1">
<div data-role="header" id = "hdrMain" name = "hdrMain" data-nobackbtn = "true">
<h1>Food for Thought</h1>
</div><!-- /header -->
<div data-role="content" align ="center">
Play
Profile
Logout
</div><!-- /content -->
<div data-role="footer">
<p>Logged in as: <?php print $_SESSION["username"]; ?> </p>
</div>
</div>
</body>
</html>
Thank you any help is appreciated.
Edit: Problem was that the file I was trying to run php code ended in .html instead of .php. Thank you all who tried to help.
Well, you're HTML page has:
session.start();
it should be
session_start();
is it possible to embed this into html
if (empty($_POST['extras'])) {
$message .="<br /> No extras selected <br />";
} else {
foreach($_POST['extras'] as $extra)
{
$message .="<br />Extras: ".$extra."";
}
}
I would like to place the above php statement at the bottom of this html code.
<!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" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Booking System</title>
<link rel="stylesheet" href="css/bs-admin.css" type="text/css" />
</head>
<body>
<noscript>
<div class="js_error">Please enable JavaScript or upgrade to better browser</div>
</noscript>
<div id="index">
<h1>Thank you for your reservation!</h1>
<p>
<h3>Your Booking is as follows:</h3>
<p>Dear <b><?php echo $custInf[0] ?></b>,
<p>You have Booked: <?php echo $eventInf[0] ?>
<p>Booking Date: <?php echo $eventInf[2] ?>
<p>Booking descriptiong: <?php echo $eventInf[1] ?>
<p>Number of machines booked: <?php echo $qty ?>
<p>Street: <?php echo $comments ?>
<p>Suburb: <?php echo $suburb ?>
<p>Postcode: <?php echo $postcode ?>
<p>Dropoff: <?php echo $dropoff ?>
<p>Duraton: <?php echo $duration ?>
If it's got php code in it then it's no more HTML.
You have to call it .php or .phtml.
PHP generates, or outputs html.
You can have pure html in .php scripts (outside the <?php ?> tags), but not the other way around (i.e. no php code in regular .html files).
If you want to add some logic (the PHP code) within it, you need to have it parsed by a webserver which will, in turn, generate html.
<!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" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Booking System</title>
<link rel="stylesheet" href="css/bs-admin.css" type="text/css" />
</head>
<body>
<noscript>
<div class="js_error">Please enable JavaScript or upgrade to better browser</div>
</noscript>
<div id="index">
<h1>Thank you for your reservation!</h1>
<div>
<h3>Your Booking is as follows:</h3>
<p>Dear <b><?php echo $custInf[0]; ?></b>,</p>
<p>You have Booked: <?php echo $eventInf[0]; ?></p>
<p>Booking Date: <?php echo $eventInf[2]; ?></p>
<p>Booking descriptiong: <?php echo $eventInf[1]; ?></p>
<p>Number of machines booked: <?php echo $qty; ?></p>
<p>Street: <?php echo $comments; ?> </p>
<p>Suburb: <?php echo $suburb; ?></p>
<p>Postcode: <?php echo $postcode ?></p>
<p>Dropoff: <?php echo $dropoff; ?></p>
<p>Duraton: <?php echo $duration; ?></p>
</div>
<?php
$message = "";
if (empty($_POST['extras'])) $message .="<br /> No extras selected <br />";
else
{
foreach($_POST['extras'] as $extra)
{
$message .="<br />Extras: ".$extra;
}
}
echo $message;
?>
</div>
</body>
</html>
I think you're needing to create an empty variable $message before you could start appending "extras" to it. Then all you need to do is echo $message.
Yes you can, however that's not a good way to do it. You should go for the Model-View-Controller model. It separates the HTML code from the actual code that does processing. Many PHP Framework does this. (Though personally i find them too clunky and wrote my own)
Also, embedding HTML into PHP is bad, as again, code should be separated from the HTML by as much as possible.
Example of views and controllers (From my framework):
Controller:
class Controllers extends BaseController{
function index($args=array()){
// process data
$this->render('index', array('data1'=>$data));
}
}
View:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>My site</title>
</head>
<body>
<h1><?php echo $page->data1; // echos out $data from the view ?></h1>
</body>
</html>
This is much cleaner than your model of embedding php into the HTML and/or vice versa.
Take a look at frameworks, they are usually pretty helpful, although PHP frameworks are generally very restrictive as to what you can do.
Yes, you can do what you are asking. Make sure the extension is recognizable by the php interpreter (usually .php)
If you need to hack something up quick, this is ok. But for anything else than that, look into using some sort of templating language. This becomes an important point because you want to seperate your logic from your display for the sanity of yourself and other developers that will work on your code in the future.
edit: oh, also very important. Don't use $_POST this way without sanitizing the data. It's ripe for XSS injections.
<!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" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Booking System</title>
<link rel="stylesheet" href="css/bs-admin.css" type="text/css" />
</head>
<body>
<noscript>
<div class="js_error">Please enable JavaScript or upgrade to better browser</div>
</noscript>
<div id="index">
<h1>Thank you for your reservation!</h1>
<!--- bunch of stuff omitted here -->
<?php
if (empty($_POST['extras'])) {
echo "<br /> No extras selected <br />\n";
} else {
foreach($_POST['extras'] as $extra)
{
echo "<br />Extras: ".$extra."\n";
}
}
?>