I have actually discovered my problem but I am really want to know why this is an issue. I had two pages form1.php I started a session on that page and hit submit. Then I had a link to session2.php which started that session and was able to pull the information from form1.php. I am just learning about sessions and this was a very simple exercise to learn what a session can do.
Here lies the issue, I had a stylesheet link in my head and it had a blank href well it was href="#" and when that was there the session2.php would not start the session from form1.php and grab the info from the form. Without that href="#" in the style tag it worked fine, and it also worked fine if it was a fake styletag href="something.css" but href="" doesn't work either.
Why is this? I only have those in because its a template I made for workflow, maybe I cant include the css link in my template anymore to prevent future issues.
You can see this site working here, if I haven't explained myself.
form1.php
<?php
session_start();
$_SESSION['name'] = $username;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title></title>
<!--//CSS STYLESHEETS//-->
<link rel="stylesheet" href="#" type="text/css">
</head>
<body>
Go to session 2
<!--form stuff is in here-->
</body
session2.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title></title>
</head>
<body>
<?php
session_start();
$username = $_SESSION['name'];
echo $username;
?>
</body>
</html>
Your second page needs to look like this:
<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title></title>
</head>
<body>
<?php
$username = $_SESSION['name'];
echo $username;
?>
</body>
</html>
Note that session_start() must appear before any content is printed to the screen.
Per the note on the session_start PHP manual page:
Note: To use cookie-based sessions, session_start() must be called before outputing anything to the browser.
To work like you want it, you need to have started the session first. Sounds simple, because it is. When you say session_start, php then looks for an accepted session cookie first to process content.
From http://php.net/manual/en/function.session-start.php
Note: To use cookie-based sessions, session_start() must be called before outputing anything to the browser.
Are you trying to output stuff to the page before you send the headers? What happens if you put the stylesheet after you call session_start()?
Related
Background: I am building an Ajax page which will be called by a user's browser to retrieve data already loaded in $_SESSION. I want to test that the Ajax page works properly so I am trying to do a print_r($_SESSION) on it. (Note: the print_r() works fine on the main site.)
Problem: I cannot get my PHP to produce valid HTML. If I do:
<?php
header("Content-Type: text/html; charset=utf-8");
echo '<?xml version="1.0" encoding="utf-8" ?>';
if (!session_id()) {
session_start();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.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" /> <!-- See http://www.w3.org/International/O-charset.en.php-->
<title>Test for Ajax</title>
<link rel="stylesheet" media="screen" type="text/css" title="StyleSheetProjet" href="StyleSheetProjet.css" />
</head>
<body>
<pre>
Content of $_SESSION:
<?php print_r($_SESSION) ?>
</pre>
</body>
</html>
In which case the page shows:
Content of $_SESSION:
Array
(
followed by a bunch of __PHP_Incomplete_Class Object (because the objects in session have not been declared I assume).
However if I start with:
header("Content-Type: text/html; charset=utf-8");
echo '<?xml version="1.0" encoding="utf-8" ?>';
//Obects in session:
require_once("Class_User.php");
require_once ('Class_Message.php');
if (!session_id()) {
session_start();
}
//(Rest is same as above)
Then the browser renders nothing at all, not even Content of $_SESSION:. In fact, the html in the web console is simply <html><head></head><body></body></html> with nothing in between...
What am I doing wrong?
Edit 1: to replace text/xml in the header() with text/html.
Edit 2: I replaced the two require_once with:
if (file_exists('Class_User.php')) {
echo "Class_User exists";
} else {
echo "Class_User doesn't exist";
}
if (file_exists('Class_Message.php')) {
echo "Class_Message exists";
} else {
echo "Class_Message doesn't exist";
}
The page does return Class_User existsClass_Message exists so it clearly can find the files.
Suspected Reason
require_once("Class_User.php"); and require_once("Class_Message.php"); are looking for the related files, cannot find them, so stop executing the rest of the code. Check if the path to Class_User.php and Class_Message.php are correct.
Actual Reason (we found working together w/ the OP after debugging)
One of the class files was extending another class and since that file was not included in the project the execution was being blocked. The OP solved the issue by calling another require() for this third class.
at the moment I'm having a tough time with HTML, PHP and Javascript! I'm trying to learn those three scripting languages at a time but there are some issues which I cannot solve on my own.
My problem is this: I've coded a PHP file. The code in it looks like this
<?php
$variable1=300;
$variable2=600;
and so on
echo'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
blabla
else{if
(blah.blah==(something)){**FUNCTION()**;}}
</body>
</html>'
?>
Now the thing is, i want to insert right into that part where FUNCTION() is stated, something along these lines:
$phpvar=20;
HOW can i insert there my PHP variable without getting any errors?!
would be really glad if someone could help me out,
thanks in advance
EDIT:
the problme is I've used the code "ECHO blahblah" so your ideas won't really help me. I've edited my source in order to use your suggestions but still i cannot figure it out...
Now my code looks like:
<?php
$variable1=300;
$variable2=600;
and so on
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
blah
<div id="blah" style="margin:0;padding:0;position:absolute;left:' . $variable1 . 'px;top:' . $variable2 . 'px;width:148px;height:57px;text-align:left;z-index:3;">
</body>
</html>
how can i insert variables to make it really work? does not work..
Well my first suggestion would be to change the way you output your data to the browser i.e.
<?php
$variable1=300;
$variable2=600;
and so on
//instead of doing an echo and echo-ing all the stuff out you just temporarily end your code
?>
then you output your HTML here and your JavaScript code, and you insert a php block when you need to input it into JavaScript like this:
var SomeVar = <?php echo $variable1; ?>;
and so on...
hope this helps
Something like this:
<?php
$variable1=300;
$variable2=600;
// and so on
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head></head>
<body>
<!-- blabla, some HTML, some JavaScript, etc. -->
if (blah.blah==(something)){
<?php
$phpvar = 20;
// some other PHP code
echo $phpvar;
?>
}
</body>
</html>
There are a number of different ways to accomplish this, and somebody may be able to answer with a way that's more idiomatic in PHP (it's been a while since I've done PHP). Essentially what I'm doing here is a couple of things:
I've separated the PHP code from the HTML markup a little bit. This is often preferred over large echo statements for readability, maybe performance, etc. There's no need to echo large chunks of static HTML, just treat it as regular HTML outside of the PHP tags. (This will make it a lot easier to develop and read as well, since you won't have to escape things or deal with PHP strings in your HTML code.)
I added a second PHP tag for the code that's in the middle of the page. I imagine you'll want to do more than just assign a variable, and you can. Anything you echo in that section of code will end up in that part of the rendered HTML output. You can also write loops around your HTML, which would repeat it just like it was part of the PHP code, etc.
The main thing is to keep the PHP code contextually separate from the HTML/JavaScript that it emits. The PHP code gets processed on the server before the page renders in the browser. Once the page is delivered to the browser, the PHP code is done and effectively no longer exists. At that point it's a plain HTML/JavaScript page being rendered and executed in the browser. While it's not clear what you're actually trying to do in your resulting HTML/JavaScript in the question, you'll want to make sure in your final code you make it clear and easy to understand and don't try to mix JavaScript with PHP. (At least not without understanding the separation and using it accordingly, such as with AJAX calls.)
Not quite sure what you code is doing, but what about this?
<?php
$variable1 = 300;
$variable2 = 600;
$blah = 'something';
$phpvar = 'I am here';
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<?php
if ($phpvar != '') {
?>
<script>
var phpvar = "<?php echo $phpvar?>";
</script>
<?php
}
?>
</head>
<body>
<p>Hello world!</p>
<?php
if ($blah == 'something'){
?>
<script>
function doStuff(){
alert('Stuff done.');
}
</script>
<?php
}
?>
<p>After stuff</p>
<?php
if ($blah != 'something'){
?>
<script>
function noStuff();
</script>
<?php
} else {
?>
<p>No stuff done this time.</p>
<?php
}
?>
</body>
</html>
http://codepad.org/4hbVRJID
Which will output:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script>
var phpvar = "I am here";
</script>
</head>
<body>
<p>Hello world!</p>
<script>
function doStuff(){
alert('Stuff done.');
}
</script>
<p>After stuff</p>
<p>No stuff done this time.</p>
</body>
</html>
Note how I use the PHP tags (<?php ?>) to interpolate the PHP within the markup. This is often done when using PHP as a templating engine (on top of any logic done separately). This way you don't have to deal with the echo, but it can sometimes be hard to read.
I was learning PHP and trying to redirect pages based on if the cookie is set or not so below is the code I used to set the cookie on the first page
<?php
setcookie("test","logged in",time()+60,'/');
?>
Now on the testing page I delete the cookie but it doesn't get deleted below is the code
<?php
setcookie("test", 0, time()-(60*60*24*7));
if(isset($_COOKIE['test']))
{
echo "u had logged in";
}
else
header("Location: index.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>Untitled Document</title>
</head>
<body>
</body>
</html>
What exactly is the problem ?
Cookies are only set when a page is transmitted to the browser and are only read when they're sent to the server as a part of a HTTP request.
As such:
If you delete a cookie, it won't disappear until the next page load.
If you set a cookie, the value can't be read until the next page load.
A common way of dealing with this is to set/delete a cookie and then perform a re-direct.
Changes made to cookies are only visible to the server on refresh. If you reload the testing page, you shouldn't see the "logged in" text.
I have a test.php page
in this page , there is a url . i need to execute that url (here database updation is doing).This is my code
<?php
$username ='testUsername';
if($_GET['age']!=''){
header('location:www.test.com/update.php?age='.$_GET['age'].'&username='.$username); //need to updatethe age of this username
$show ='hello';
}
?>
<!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=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<?php echo $show ?>
</body>
</html>
i know this code is not going to work properly.How can i write in a good way.I donot want to redirect the page.I just need to execute that link
Just call file_get_contents on the url in question instead of setting the location header to that value.
Since the update.php file is hosted on an external website, the only thing you can do is get the contents of the output of the file. (Use file_get_contents to get that output.) That is, it will call the file (with your parameters) and you can fetch the HTML result of it—nothing more. It would be a major security problem if server files could be executed on external websites.
If you need to include the code in update.php without redirecting, you can do so with the include or require functions.
require() is identical to include()
except upon failure it will ... halt
the script whereas include() only
emits a warning (E_WARNING) which
allows the script to continue.
Use the IMG tag. <img widht=0 height=0 src="<?='location:www.test.com/update.php?age='.$_GET['age'].'&username='.$username ?>" />
This include is not working in IE:
<?php
include_once 'localization.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>Global Colleague</title>
<link href="css/style.css" rel="stylesheet" type="text/css"/><!--Start Kampyle Exit-Popup Code-->
<script type="text/javascript">
Calling an array inside localization.php
<?php echo l('content_p3'); ?>
EDIT: I tried the same file in another folder and it worked
"Not working" is very generic. What is it that you expect and what is actually happening? You may want to turn error reporting on and see if any error is reported. In (X)HTML, nothing should be printed before the doctype. Are you trying to add something to the HTTP response? Typically, the browser shouldn't effect how PHP outputs your code unless you've added some code to respond to the user agent which is not always wise.
Perhaps, when you tried your code in another directory it wasn't able to find the offending script as it is included by a relative path. Try removing the include in the original file and see if it "works".
The following way 100% work for you:
1) Install (Open Source) Notepad++ On your PC
2) Open the file in it
3) Go to encoding and select (Encode in UTF-8 Without BOM)
4) Then click save
5) Now it can work on (Chrome) and (IE)
:)