If I have a page with multiple <?php ... ?> sections interspresed with pure HTML sections. I notice that a $_SESSION varible set in one <?php ... ?> section is not available in another on the same page.
So, what's the best practise?
1) call session_start() as the first line of each <?php ... ?> section?
2) only have one <?php ... ?> section which covers the whole page? If so, I have to wrap each HTML section in echo, which is annoying of they are HTML form elements. Maybe heredoc them?
It's my first time to try this sort of thing, but I am not the first one to do so - what's the accepted best practise?
Edit: Aplogies, my stupid fault. One of the sections PHP started with <? and not <?php
If I have a page with multiple sections interspresed with pure
HTML sections. I notice that a
$_SESSION varible set in one section is not available in
another on the same page.
The sections of php tags <?php ... ?> have nothing to do with session. Make sure that you put:
session_start()
on top of your page.
As long as you set the header before doing any output, you shouldn't have any issues with the session (as the function session_start() also set the header).
There is really no problem having multiple PHP sections on a page. But I would highly recommend, to do all the logic (reading from database, processing form data) in a separate file OR in the first section.
Related
I absolutely don't post a question here in SO unless I really can't find a way to solve my problem myself. I did a lot of googling and was not able to find a solution for this one problem I am about to describe.
Here is the problem. I am creating a templated php website. With templated I mean something like below:
<?php include("header.php");?>
<div id="content">
<div id="main">
<h2><?php echo($page_title);?></h2>
<?php
echo ($page_content);
?>
</div>
<?php include("sidebar.php");?>
</div>
<?php include("footer.php");?>
As you can see here page template ECHOES the content of the $page_content variable between header and footer sections to build the page.
To keep the code clean and separated (in my own way) I have been placing the html content in .txt files (let's say page1_content.txt) and assigning the txt content to this variable ($page_content) as below:
$page_content = file_get_contents("page1_content.txt");
My problem starts when I place some php code in page1_content.txt, lets' call this file page2_content.php (yes, I change the file from .txt to .php). Then I assign the content of this file to $page_content variable as below as usual:
$page_content = file_get_contents("page2_content.php");
Now, when the page template ECHOES page2_content.php contents the php code in it is also echoed as string and not executed, but I am trying to query a database and do some stuff in this file with some php code. I mean, I want the php code inside page2_content.php to be executed and the cumulative html code to be echoed by the "echo" line inside the template file.
How can I achieve this?
Please ask me any questions if you need more info/clarification.
Thanks
EDİT:
As many people here suggested the solution was including the file. Actually, I tried including the file before but it didn't look like it was working, it broke my template, so I though I was on the wrong track and quit the "include" way of doing this. Since everybody here is advising to use include I tried that again. I replaced the php code in "page2_content.php" with a basic 1-line code just to see if it gets executed before adding generated html code without breaking the template and it worked. Apparently my php code had a problem at first place and hence broke my template execution.
Now I have changed the template structure slightly and pages using the template, and it seems to work nicely. Thanks a lot everybody. I have up-voted every answer suggesting that I use include :)
As #Ali suggested, you could include the files. The other option which I highly suggest you do not use is the eval() function.
I think what you want to do is to include your content PHP file, not echo it (as you are doing with header.php and footer.php).
echo($page_content);
Would become as below:
include("page2_content.php");
You've already done this in your footer and sidebar, just use include()
This question already has answers here:
When do I have to declare session_start();?
(2 answers)
Closed 9 years ago.
So I'm starting my own website and I have the login file pretty much made. I just need to figure out where to put the session_start to keep the user logged in. Where exactly do I put the session_start? Do I put it right in the login file? Or where do I put it?
Thanks for the help
Put it after your PHP start tag <?php ... like this
<?php
session_start();
//... your code....
//more code....
Read more on sessions from the PHP Manual. Here
Note : Also keep in mind, you need to call session_start(); on each and every page if you are making use of session variables.
Put it right after the start tag, or else headers will have been send, and the session, AFAIK, has to be the first header sent
<?php
session_start();
//session code here
?>
Right after <?php tag.
Be sure that there is NO output before this function (even a space symbol or so).
You want to put session_start(); at the top of your page before any other code. However, if you are using includes to make your life easier, it's best to put it at the very top of a file that is included in all files. For instance, when I make a website, I put all of my header code and footer code in separate files and include them in the other files. I also have a functions file that is included in every other page of the website. So for my index file, it may look something like this:
<?php include_once("includes/header.php"); ?>
<div id="content">
Website Content
</div>
<? include_once("includes/footer.php"); ?>
Then, my header file would start like:
<?php include_once("includes/functions.php"); ?>
<!doctype html>
<html>
<body>
Then at the top of my functions file:
<?php session_start();
[functions]
?>
In this way, the functions files' code gets ran first, therefore the session start code is the very first thing hit. Why? You cannot have any type of output to the browser before starting a session.
it's better to have a separate file other than your login to do some common stuffs.
i think your login file will be generally handling user verification and validation thing. so don't include that file on every page.
have one more file that
includes all required files
keeps all your analytic scripts
initializes global variables
and this file you can start with <?php session_start(); ?>
session_start() needs to go in every page/file that refers to $_SESSION (obviously the login page is included).
Because you should only be calling it once, I tend to write a lazy_session_start() method (and tend to put it in an include file):
/**
* Lazily calls session_start (to prevent warnings).
*/
function lazy_session_start() {
if (!isset($_SESSION) || !is_array($_SESSION)) {
session_start();
}
}
It could be called like so (before you need to use $_SESSION):
<?php
//you must either declare "lazy_session_start" function
//or import the file containing the function definition.
require_once('lazy_session_start.php'); //or something.
lazy_session_start();
//... you may now use the $_SESSION array.
I have a problem with my self-coded template system. The content is inserted by Include(). Now I need to add a meta redirect to one page. I know, meta redirect is not the safest way, but I need it because of it's delay possibility.
Now i'm looking for a way to influence the wrapping page (template) by the wrapped page (content).
So I thought a function can do this job.
<?php function test($testvar){
echo $testvar;}
?>
<hr />
<?php
test("testtext");
?>
Of course the text echos in the line of the function call, not in the function line. Is there a way to make the function echo in the line of the function itself? In this case above the horizontal rule, not below?
Of course every other best pratice for this "template problem" ist welcome!
THX
EDIT:
<html>
<title>testpage</title>
<? a_function_that_echos_a_metatag_for_redericetion($param){
echo $param;
}
?>
<head>
<body>
<? include("test.php");
INCLUDED script---->
a_function_that_echos_a_metatag_for_redericetion("\"<meta http-equiv=\"refresh\"...")
Procedural programming cannot have the logic you request.
If you want to add the meta redirect element before the page content, you HAVE to know that you are GOING to make this redirect before the <head> is closed.
Is there a way to make the function echo in the line of the function
itself?
No. Functions are declarations and aren't executed until they are called.
If you wish to modify the request headers later down the page, then you'll need to employ output buffering. However, this approach will likely be overkill if only one page needs the special header consideration. It may be better to just detect the page via some parameter (such as REQUEST_URI) and insert the header conditionally from that.
I have 2 pages of html: a.html & b.html.
a.html contains text "hello" and a link to b.html.
When I click the link to b.html, I want the "hello" text to also come on specific place in the b.html page.
How can i do this? Kindly let me know.
To bring it along, you'll need to pass it somehow. It could be something you keep in an input element on the screen, then when you submit it (as a form) it will be available as a $_REQUEST element. You could also do this by putting your desired welcome message in a hidden element and submitting it in the same manner.
Alternatively, you could put the value you want in a URL string manually. For example, the link in a.html to go to b.html could be called by:
<a href="b.html?welcome=welcome%20to%20the%20show">
Doing things on the URL string is more PHP than html, and if your server's not setup correctly you may not be able to get values from the URL string in an HTML document. So, in that case it's probably easier just to make it a .php file. Again, not that it can't be done, but it might not be setup to work that way by default.
.html pages are static, so you'd have to copy the desired text manually into the second page.
If you want to do it via PHP, then the simplest way is to put the common text into a file, like:
my_html.html:
<p>This is the common text which should be included in both pages</p>
and
a.php:
... lots of stuff ...
<?php include('my_html.html'); ?>
... more stuff ...
and
b.php:
... other stuff ...
<?php include('my_html.html'); ?>
... more other stuff ...
It sounds like you want to "include" some common text between multiple pages. If you're limited to client-side functionality, it's possible to do this by writing some Javascript to populate an HTML element with a string via DOM, and then include that javascript on both pages. Both HTML pages would have to define the target element.
If you have server-side technology flexibility, I would suggest you look at PHP. Implement both pages as .php pages, and use include() to include a common file (e.g. common.php) in both pages.
You could use php sessions and store the text there so e.g.
a.html
<?php
session_start();
$_SESSION['session_name']="Hello";
echo $_SESSION['session_name'];
?>
b.html
<?php
session_start();
echo $session['session_name'];
?>
A session is more or less a cookie and what you are doing here is storing the string "Hello" in a cookie name "session_name". You can then display the cookies content by using the php echo command :)
I am calling this function halfway down the page:
<div id="leftArea">
<?php
if (isset($id)){
single($id);
}
?>
</div>
The problem is i want use some of the output in the meta tags in the <head>, whats the best way to approach this?
EDIT:
The single function, takes the $id and echo's the data straight to the page, exactly where it is. In the DIV leftArea. But i want to take one of the rows from the DB and insert it into the META tags at the top
Copy the code into the <head> section.
Redesign your System
The best method is to create a class that manages your html page for you, example:
$Page = new HTMLPage("My Page",HTMLPage::Strict);
$Page->addScript("....");
$Page->addScript("....");
$Page->addScript("....");
$Page->addStyle("....");
$Page->addStyle("....");
$Page->addStyle("....");
$Page->SetBody($MyTemplate);
$Page->send();
this way though out your functions you can do
function myfunc()
{
global $Page;
$Page->addScript("....");
}
the main point here is you should build your document up before sending it to the browser, this way you still have control over the content no matter where your code is executing from.
on the final send method you build your content up, and then push the content via echo, and then exit directly. (all processing should be done prior to output to manage errors)