Cannot regenerate session id zend framework 2? - php

I used ZFcuser module for my admin panel . when I login into admin panel its returns error like this :
Warning: session_regenerate_id(): Cannot regenerate session id - headers already sent in /home/public_html/dev/vendor/zendframework/zendframework/library/Zend/Session/SessionManager.php on line 260
This works fine in my local windows system but its returns such warning while run in my linux hosting server.
Need help to solve this issue. Thanks in advance.

You can regenerate your session id with the ZF2 SessionManager:
$manager = new Zend\Session\SessionManager;
$manager->regenerateId();
However, your error is likely not caused by this function, but because of something else. Session management (either session_start() or regenerating session ids) is only possible when you haven't used echo or something similar before that piece of code:
As an example, this will fail:
<?php
echo 'Hello';
session_start();
On the other hand, this will just work:
<?php
session_start();
echo 'Hello';
You have to be very careful with opening and closing php tags. If you have any closing php tag in your classes, remove them. If there is blank line after this, it's interpreted as text which is returned in the response and this causing troubles with sessions. This will fail as well:
<?php
// Do some work
?>
<!-- blank line -->
<?php
session_start();
?>
So: check all your code for echo, print etcetera. Also, check for any closing php tag in your source code. If all above things won't help you, post the piece of code where you are regenerating your session id, that might give some more insights.

This error typically appears when you dump some characters to PHP standard output. This results in sending the HTTP headers to web browser, so you cannot add a session header anymore.
To resolve the issue, ensure you do not echo or printf any character before you start the session. Also ensure that your PHP class files do not have closing ?> tags. If you have some characters after the ?> tags in your PHP class files, those characters will be dumped to stdout, breaking your session.

Please check following
protected function _initSession() {
$config = array(
'name' => 'session',
'primary' => 'id',
'modifiedColumn' => 'modified',
'dataColumn' => 'data',
'lifetimeColumn' => 'lifetime',
'lifetime' => 60*60*24*30,
);
Zend_Session::setSaveHandler(new F_Session_SaveHandler_DbTable($config));
}

Related

error using zebra_form in a wordpress plugin

I'm trying to use Zebra_form in a metabox for a Wordpress plugin I'm working on.
For simplicity I reduced the code to this:
public function render_metabox( $post ) {
// instantiate a Zebra_Form object
$form = new Zebra_Form('form');
// the label for the "email" field
$form->add('label', 'label_email', 'email', 'Email');
// add the "email" field
$obj = $form->add('text', 'email', '', array('autocomplete' => 'off'));
// auto generate output, labels above form elements
$form->render();
}
But I get the following error when I try to run it:
Warning: Cannot modify header information - headers already sent by (output started at /Users/dp/Documents/SitesDev/wordpress/wp-admin/includes/template.php:1877) in /Users/dp/Documents/SitesDev/wordpress/wp-content/plugins/my-plugin/lib/Zebra/Zebra_Form.php on line 4052
Fatal error: The library tried to store the CSRF token in a cookie but was unable to do so because there was output already sent to the browser. You should either start a session prior to instantiating the library (recommended), have no output (including and tags, as well as any whitespace) sent to the browser prior to instantiating the library, or turn output buffering on in php.ini. in /Users/dp/Documents/SitesDev/wordpress/wp-content/plugins/my-plugin/lib/Zebra/Zebra_Form.php on line 4052
Any clues? Or Zebra_form cannot be used in this way?
I know it has been a while since you posted this question but here is where I came up with
I got the same error as you so I looked in the Zebra_Form.php and commented out line 4041 to line 4068.
It worked after.
you have to put
session_start();

PHPQuery WebBrowser plugin - using cookies

I'm trying to login into a website using PHPQuery's WebBrowser plugin. I'm able to successfully login but I'm not sure how to reuse cookies from a previous call to the next.
$client = phpQuery::browserGet('https://website.com/login', 'success1');
function success1($browser) {
$handle = $browser
->WebBrowser('success2');
$handle
->find('input[name=name]')
->val('username');
$handle
->find('input[name=pass]')
->val('password')
->parents('form')
->submit();
}
function success2($browser) {
print $browser; // prints page showing I'm logged in
// make authenticated requests here
}
How do I make other requests with session/login cookies?
I've taken a look at the source code to aid you with this problem. My first impression was that the code was very poorly written. Debugging code commented out, typos all over the place, mile-long functions, etc. You really might want to consider switching to a different solution in the long run because if the author changes something in this code, you might end up having your own code broken with an upgrade.
That being said, the WebBrowser plugin gives you access to the browser object itself, which contains a function called getLastResponse(). This returns a Zend_Http_Response object, which you can theoretically use to get the cookies.
The problem is that you don't have any way of setting those cookies. You would have to patch the web browser plugin somewhere around line 102 to include your own HTTP request object (parameter 2 for phpQuery::ajax()) with your cookies set, around here:
$xhr = phpQuery::ajax(array(
'type' => 'GET',
'url' => $url,
'dataType' => 'html',
));
Alternatively you could also patch phpQuery.php line 691 to include a global cookie jar you could define as a singleton or so. (Right where it says $client->setCookieJar();).
Again, this code is very poorly written, you are probably MUCH better off using raw curl calls, even if it lacks a bit of functionality.

PHP Prints everything after => (wamp)

I've got a really wierd problem on my hands...
When I try to require a class of mine everything after the use of => gets printed to the page instead.
There's really no problem in the code, it worked previously when I initially developed it on a linux machine but now when I try to use it in wamp it doesnt...
So my best guess is that it's a wamp problem although => works fine in other classes.
I'll paste the code where this occurs anyway.
index.php:
<?php
require_once('classname.class.php');
Classname::getInstance();
classname.class.php:
<?
class Classname
{
protected static $defSettings = array(
'TITLE' => 'My site'
);
other code...
So when I load the page it prints out:
'My site' );other code...
And a fatal error: "Class 'Classname' not found in C:\wamp\www\site\index.php on line 4"
What on earth could cause this?
EDIT: solved. Stupid "typo" error.
Every PHP code block MUST be encased with <?php ... ?> code tags, even if you're using include() or require().
Remember, there is no such thing as a "PHP script". There are only files that contain PHP code blocks. Without the <?php ... ?> tags, the PHP interpreter will NOT treat the text as code.

Ajax issues, Invalid JSON

I'am building simple Ajax application (via jquery). I have strange issue. I found where the problem is, but I don't know how to solve it.
This is simple server-side php code:
<?php
require('some.php');
$return['pageContent'] = 'test';
echo(json_encode($return));
?>
On the client side, the error "Invalid JSON" is thrown.
I have discovered that if I delete require function, everything work fine.
Just for information, the "some.php" is an empty php file. There is no error when I open direct php files.
So, conclusion: I cant use require or include function if I want to use ajax?
Use Firebug to see what you're actually getting back during the AJAX call. My guess is that there's a PHP error somewhere, so you're getting more than just JSON back from the call (Firebug will show you that). As for your conclusion: using include/require by itself has absolutely no effect on the AJAX call (assuming there are no errors).
Try changing:
<?php
require('some.php');
$return['pageContent'] = 'test';
echo(json_encode($return));
?>
To:
<?php
$return = array(
'pageContent' => 'test'
);
echo json_encode($return);
?>
The problem might have to do with $return not being declared as an array prior to use.
Edit: Alright, so that might not be the problem at all. But! What might be happening is you might be echoing out something in the response. For example, if you have an error echoing out prior to the JSON, you'd be unable to parse it on the client.
if the "some.php" is an empty php file, why do you require it at all?!
require function throws a fatal error if it could't require the file. try using include function instead and see what happens, if it works then you probably have a problem with require 'some.php';
A require call won't have any effect. You need to check your returned output in Firebug. If using Chrome there is a plugin called Simple REST Client. https://chrome.google.com/extensions/detail/fhjcajmcbmldlhcimfajhfbgofnpcjmb through which you can quickly query for stuff.
Also, it's always good to send back proper HTTP headers with your response showing the response type.
It's most likely the BOM as has been discussed above. I had the same problem multiple times and used Fiddler to check the file in hex and noticed an extra 3 bytes that didn't exist in a prior backup and in new files I created. Somehow my original files were getting modified by Textpad (both in Windows). Although when I created them in Notepad++ I was fine.
So make sure that you have your encoding and codepages set up properly when you create, edit, and save your files in addition to keeping that consistent across OSes if you're developing on windows let's say and publishing to a linux environment at your hosting provider.

Headers already sent error in CakePHP app [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 9 years ago.
function new_photo()
{
if( !empty($this->data))
{
$this->data['Photo']['showcase_id'] = $this->Session->read('CurrShowcase.id');
$this->data['Photo']['added'] = date("Y-m-d H:i:s");
$this->Showcase->Photo->save($this->data);
$flasher = 'Photo uploaded successfully';
$flasher .= '<br/><img src="' . $this->data['Photo']['thumbnail_url'] . '"/>';
$this->Session->setFlash($flasher);
//$this->redirect(array('action'=>'sc',));
}
}
I have a Showcase Controller in my CakePHP app, and a new photo form to submit new photos. Whenever I uncomment the last line that redirects after the data is saved, I get this error:
Warning (2): Cannot modify header information - headers already
sent by (output started at D:\.....
Even if I get this error, $this->data still gets saved properly in the database. However, if I comment the redirect line as shown above, everything works fine and error-free. I HAVE checked for blank spaces around the tags, so I'm pretty sure it's not that.
Any ideas?
Edit:
commenting out the setFlash statement does not fix the problem.
Change your debug mode to 0 to make sure it's not a notice/warning being generated prior to the redirect. Also, you might want to tighten up your processing section to (be paranoid and) ensure that it's not using invalid indexes, as well as anywhere else throughout the application flow for that action to make sure you're not getting any ouput (if it works when you change debug to 0).
Is there a debug statement somewhere that you're not showing us?
You may be up against an invisible UTF-8 BOM character somewhere. Check your text editor settings whether it saves your files with BOM or without.
I'd check for whitespace in the models. Anyone of them. That was one of the gotchas I hit.
Either this code outputs something to the browser, or you have a whitespace after ?> in the end of the file (or any other included file). The whitespace is sent to the user thus sending http header.
I'm assuming setFlash outputs something to the browser?
If whitespace before or after your <?php ?> tags isn't your issue you might have to try passing 'null' for the 'layout' parameter of setFlash();
i.e.
$this->Session->setFlash($flasher, null);

Categories