I'm trying to work with Zend Sessions and after failing for a while, I tried basic counter example:
$defaultData = new Zend_Session_Namespace('language');
if(isset($defaultData->counter))
{
$defaultData->counter = 1;
} else {
$defaultData->counter++;
}
echo $defaultData->counter;
But on each page refresh I get the value 1. I'm calling Zen_Session::start in Bootstrap autoloader. What are the possible ways to debug, solve this problem?
Your if-else is the wrong way around. Now, if there is a session variable, you assign the value '1'. So, change statements for if and else and you will be fine.
Related
I am new at laravel , php , the user had specific language to use and I deleted this language from admin panel , this result of course in the user can not login to account and we get this error . I create the lang file again but not working.. now I got this error and need help to fix this issue, it helps me to learn more about laravel and PHP. please help a brother out
$lang = DB::table('languages')->where('id', Auth::user()->language)->get();
App::setLocale($lang[0]->lang_code);
I would suggest you to do something like this:
$lang = DB::table('languages')->where('id', Auth::user()->language)->first();
if ($lang) {
App::setLocale($lang->lang_code);
}
You have to check either $lang has some data or not then you can fetch its lang code, please try as below
if(count($lang)) {
App::setLocale($lang[0]->lang_code);
}
It's because your query is not fetching any records (because you delete it), and you're calling the index 0 which is the array key if your query gets some records.
There are several ways to fix this kind of issue.
For example:
if($lang->count() == 0) {
//set default language
App::setLocale('en');
} else {
//if has records select the first row
App::setLocale($lang[0]->lang_code);
}
Also, if you want to retrieve a single record I recommend using first() instead of get().
I have the problem that my data variable is suddenly null.
It's happening directly after an IF-Statement when nothing was written to this variable. Does anyone knows what it is happening here?
public function render()
{
ob_start();
if($this->ajax)
$ext = '.ajax';
else if(file_exists($this->scriptPath.$this->template.'.mst'))
$ext = '.mst';
else
$ext = '.phtml';
#var_dump($this->data); // <-- is filled with many data
if($ext === '.mst'){
var_dump($this->data); // <-- is null
$mustache = new \Mustache_Engine(
array(
'escape' => function($value){return $value;},
'partials_loader' => new \Mustache_Loader_FilesystemLoader(dirname(__FILE__).'/../../../frontendTarget/classes/lib/de/preis/frontend/viewFragments/partials',array('extension' => 'mst'))
)
);
$content = file_get_contents($this->scriptPath.$this->template.$ext);
$content = $mustache->render(($content),$this->data);
echo $content;
} else {
include $this->scriptPath.$this->template.$ext;
}
return ob_get_clean();
}
I've here two var_dumps(). One before the if, where the var is filled with data and one after the if, where the data is suddenly completely gone.
Could anyone assist me on this one?
Thanks in advance
More of a learned lesson than an answer!
I had a problem like this once that seemed like a real mystery, it took me while to figure out because it didn't make any sense at the time. My answer was that I was running my code with netbeans debugger and I had previously set a watch to clear (unset()) a variable to allow me to debug a code segment - I'd forgotten to remove the watch so it was executing during my debug session and nulling my variable
Thanks to #Wee Zel for the hints.
I double checked my code and figured out that the class method was called twice. In the first case, the data were given, in the 2nd it was not.
In my particular case, I had a MVC model where the layout was rendered first (with data) and then the view (without data). I needed the data in the view and not the layout. So I just had to pass the data to the correct layer.
I am having an issue using isset to display content on a page.
My PHP file is called messages.php
I am directing my users with links to this URL: messages.php?inbox using if(isset($_GET['inbox']))
{ } to display the users inbox. Same principle with the other users options such as compose message is: messages.php?compose again using isset
The only problem I have is that I cannot stop people from manually typing stuff like domain.com/messages.php or domain.com/messages.php?somethingrandom.
Is there a way to direct users to messages.php?inbox when they type in the address bar something that isnt assigned to isset?
I did try to use switch but couldnt seem to get it to work properly with how ive laid out my HTML.
An example of the whole file is here http://pastebin.com/SfqN2L7g
I am fairly new to PHP and think I may have gone down the complicated route.
Any advice would be appreciated.
Thanks
The answer you added already would work, but I usually like having an array of valid options which I could maybe check against later on.
$validPages = array('inbox', 'compose');
$pageFound = false;
foreach ($validPages as $validPage) {
if (isset($_GET[$validPage])) {
$pageFound = true;
break;
}
}
if (! $pageFound) {
header('Location: /messages.php?inbox');
}
Thanks to the help of Marcos PĂ©rez Gude, the answer is as follows:
if(isset($_GET['inbox']) || isset($_GET['compose'])){
//Then do below
}else{
header("Location: messages.php?inbox");
exit;
}
I'm trying to set a session variable to record when people have voted but PHP is flat out refusing to set it. The code is as follows:
elseif (isset($_GET['group']) && isset($_GET['vote']))
{
include_once(_INC.'otherheader.php');
groupVotePage($group, $vote);
$_SESSION[$group] = '1';
echo $_SESSION[$group];
}
Nothing. The function groupVotePage adds the vote to the database and echoes a thanks message. $group is the name of the group being voted for. I have session_start(); at the top of the page and have tried to declare the variable inside the function called as well, putting session_start(); everywhere. Session variables are used elsewhere on the site so I know it's not a server issue, and it's the same on all browsers I tried.
Declaring the session var inside the function works but only within the function - it doesn't go global.
if(!isset($_SESSION[$group])) {
$totalVote=$totalVote+$vote;
$totalNumVotes=$totalNumVotes+1;
$totalRating=round($totalVote/$totalNumVotes);
$totalScore=$totalVote*$totalNumVotes;
...db stuff...
$mysqli->query($query);
echo'Thanks for voting!';
}
else {
echo'You have already voted for this group!';
}
Maybe you can use PHP error reporting code to figure out where you going wrong
// Report all errors
error_reporting(E_ALL);
Okay fixed it, the < !DOCTYPE HTML> section was before the session_start();, when I put it after the code worked.
Let me first say I've spent a day reading three google pages of articles on this subject, as well as studied this page here.
Ok, here's my dilemma. I have two functions. Both called upon via AJAX. This first one assigns a value to the variable and the second one uses that variable. Both functions are triggered by two separate buttons and need to stay that way. The AJAX and the firing off of the functions work fine, but the variable isn't passed. Here is my code:
if( $_REQUEST["subjectLine"] ) //initiate first function
{
$CID = wpCreateChimpCampaign();
echo $CID; //this works
}
if( $_REQUEST["testEmails"] ) //initiate second function
{
echo $CID; //does not return anything but should contain "apple"
wpSendChimpTest($CID);
}
function wpCreateChimpCampaign () //first function
{
$CID = "apple";
return $CID;
}
function wpSendChimpTest ($CID) //second function
{
echo $CID; //does not return anything but should contain "apple"
}
I'm open to using a class but I haven't had much luck there either. I was hoping to solve this issue without using classes. Thanks for the help in advance!
If you are making 2 separate calls to this file, it may be helpful for you to visualise this as being 2 functions in 2 totally separate files. Although they exist in the same PHP file, because they used called in different calls, they don't retain the value of the variable $CID. Once the file has run, the variable is destroyed and when you call the file again, the value is null again.
So you need to store that variable between calls. You can either store it in a database or store it in a session variable.
So call session_start(); at the beginning of the file, then rather than use $CID, just use $_SESSION['CID'];
I'm not sure where the hold up is. The code you have will work:
$CID = wpCreateChimpCampaign(); // returns 'apple'
wpSendChimpTest($CID); // echos 'apple'
The code looks fine, but are you certain that all requirements are being met so both functions execute?
In other words are you supplying values for both $_REQUEST["subjectLine"] and $_REQUEST["testEmails"]?