Jump to content

Php In Body


Sericet

Recommended Posts

Hey everyone,

New to the forum and I php.

I am using adobe muse to design my website which does not allow me to write code at the beginning of the document. Adobe muse exports all the files at HTML files and then I upload them.

 

I am making a login system and I need to use sessions. I have the session working (I think). I set an variable session(email) and if this is set then echo welcome back. If it isn't set then i echoed the forms for a login.

 

Problem I am having is when I go to a link on my page my page seems to lose my session. I am unable to write my php before any HTML code. Is this causing the problem?

Link to comment
Share on other sites

Ok so, adobe muse doesn't allow me to write code at the beginning of the document. I opened it in dream weaver and put the code in before uploading it. Seems I need to have session strt at the VERY beginning of y code.... This is annoying to open it in dream weaver to add the code. Is there another option?

Link to comment
Share on other sites

Why would I have to use another IDE? I have this thing reading an IPN from PayPal and recording all data to MySQL, sessions are working problem is I can't write the sessions at the very beginning of my page. My question is, is there another way to make sessions work without having it at the top of the page.

Please explain why you think I need another IDE. Intelligent answers only please.

Link to comment
Share on other sites

First, snark like:

 

Intelligent answers only please.

 

only makes people not want to help.

 

Now, people are telling you to use another IDE because, evidentally Adobe Muse isn't meant for PHP editing. Note that no one has suggwsted that your brother also switch IDEs. Since, apparently, Muse just outputs HTML (and likely CSS as well), there's no reason why your brother couldn't do his part in Muse and you do your part in something else. Right tool for the job and all that.

 

Regarding PHP, I'm going to go out on a limb and guess that you learned what you know from a poor and/or dated resource (like w3schools, which NO ONE should go to). I'm also going to guess that the resource's examples highlighted PHP's ability to go between PHP code and HTML. I'm guessing this because you want to put PHP code in the body of your pages.

 

That's exactly the wrong way to write PHP.

 

The best way to write PHP is to do all your scripting upfront, store your results in variables, and then use as little PHP as possible in your HTML to echo those results. For you, this means all session and database work first.

 

Now, to your actual problem - do you have session_start() on each page? Can you post your code?

Link to comment
Share on other sites

NO.

 

For what it's worth, the following code works for me?

 

<html>
<head>
<title>Test Session</title>
</head>

<body>
<?php
session_start();
if(isset($_SESSION['test'])) {
print '<p>Session var found: ' . $_SESSION['test'] . '</p>';
} else {
$_SESSION['test'] = 'hello';
print '<p>Session var now set to ' . $_SESSION['test'] . '</p>';
}
?>
</body>
</html>

 

While digging through the PHP manual (http://php.net/manua...ssion-start.php), I didn't see any mention of session_start() needing to appear at the top.

 

Based on the contributor notes, however, there are some comments where session_start() didn't work without it being at the top...so maybe it comes down to server configuration.

Link to comment
Share on other sites

For what it's worth, the following code works for me?

 

It may work, but if you had error reporting on you would be getting an error message

 

While digging through the PHP manual (http://php.net/manua...ssion-start.php), I didn't see any mention of session_start() needing to appear at the top.

 

session_start(); must be called before any output or you get errors.

Link to comment
Share on other sites

It may work, but if you had error reporting on you would be getting an error message

 

session_start(); must be called before any output or you get errors.

 

 

I actually don't get any errors, notices, or warnings.

 

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>
<html>
<head>
<title>Test Session</title>
</head>

<body>
<?php
session_start();
if(isset($_SESSION['test'])) {
    print '<p>Session var found: ' . $_SESSION['test'] . '</p>';
} else {
    $_SESSION['test'] = 'hello';
    print '<p>Session var now set to ' . $_SESSION['test'] . '</p>';
}
?>
</body>
</html>

Link to comment
Share on other sites

@cyberRobot, at the top of the page, means before you output anything to the browser -

 

Note:

To use cookie-based sessions, session_start() must be called before outputing anything to the browser.

 

Your code worked, because php has a setting, mentioned by kicken in this thread, that hides incorrectly coded pages, but results in code that is not portable between different server configurations and should be avoided.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.