question in sessions and array - php

i have session code
it's working fine
but some friend told me this code should not work !
but it's work fine with me !
i try code on localhost
so my question is , if i uploaded this code to my server
it will work fine like localhost ?
or will not work like my friend said ?
my code is
session_start();
$_SESSION['news'][] = 'First';
$_SESSION['news'][] = 'Second';
print_r($_SESSION['news']);
its print
Array ( [0] => First [1] => Second )
and that's what i want !
it's fine ..

Yes it will work definitely.
Or to be more safe you can do like following before assigning it values.
$_SESSION['news'] = array();

Your friend is wrong. You can use arrays as session variables.
For supporting arguments, please see array as session variable and Can I Store An Array In A Session?.

It seems okay to me. There's no reason why it shouldn't work on your server.

After 5 hours of tests ,,
it's seems not work like my friend said !
he was right
for first time i used this way , i got an error
but i ingored it , because it's did not happin again !
but the error is show in first time only !
i browsed the page from another browser
i got that error !
i forget to save the error , but it's say you can not use empty []
after all , iam now use it as numbers
and thank you again and sorry for my language

Related

Reading a cookie that contains an array

I have a cookie set in the format:
{"necessary":true,"functional":true,"advertising":false,"performance":true,"lastConsentReset":null}
or as it is in the dev console in Chrome:
%7B%22necessary%22%3Atrue%2C%22functional%22%3Atrue%2C%22advertising%22%3Afalse%2C%22performance%22%3Atrue%2C%22lastConsentReset%22%3Anull%7D
I would like to use PHP to read this cookie and return the value of the key "advertising".
I have looked around to try and find a solution but they all just show how to read a cookie with just one piece of data like this:
return $_COOKIE[ 'somecookie' ];
I imagine I need some sort of loop to search the array I just don't know how to do it.
Any help greatly appreciated!
thanks.
Its JSON and URL encoded, just reverse it:
echo json_decode(urldecode($_COOKIE['somecookie']))->advertising; // nothing as its false

Call to member function getResourceId() on non object - Magento CE

Firstly I'd like to apologise in advance if this is a stupid question - Although I have been writing PHP for the last 12 years, I work on bespoke applications and have only been using Magento for the last hour, so I'm clueless on its structure. If possible I'd like to avoid having to dig too much into Magento as the entire reason why I'm using Magento is to save time.
Anyway, the error:
Call to a member function getResourceId() on a non-object in /var/www/[site]/lib/Zend/Acl.php on line 1174
This error occurred after attempting to log in to the administration area immediately after install. The error is in the &_getRules method in lib/Zend/Acl.php.
So, I did a little digging and $resource (the variable it is attempting to call the &_getRules method on) is actually an array - which I found odd as the this is the method definition:
protected function &_getRules(Zend_Acl_Resource_Interface $resource = null, Zend_Acl_Role_Interface $role = null, $create = false){
So, to verify that I wasn't actually losing my mind I wrote the following as the first line of the &_getRules method
if($resource!==null&&!$resource instanceof Zend_Acl_Resource_Interface){
die('<pre>'.print_r($resource,1).' </pre>');
}
Which printed:
Array
(
[G1] => Array
(
[instance] => Mage_Admin_Model_Acl_Role_Group Object
(
[_roleId:protected] => G1
)
[parents] => Array
(
)
[children] => Array
(
[U1] => Mage_Admin_Model_Acl_Role_User Object
(
[_roleId:protected] => U1
)
)
)
[U1] => Array
(
[instance] => bea423c23f6343e2b509fb192a00826f:3EOt7Vo0agtENPH8Wm73EOelgSDoDxYO
[parents] => Array
(
[G1] => Ryan
)
[children] => Array
(
)
)
)
So yeah, i guess my question comes in four parts:
Why doesn't this work out of the box?
Am I just being an idiot?
Why isn't PHP preventing this method from running considering the $resource variable is neither a Zend_Acl_Resource_Interface or null?
How do i fix it?
I should mention that this is a local test site, running on Ubuntu 12.04, Apache 2.4.9 (likely to be nginx in production though), PHP 5.5.12, Magento CE 1.9.0.2.
Thanks in advance,
Ryan
/*************************UPDATE*********************************/
Hi again everyone,
So I've noticed there is a pattern to how this problem/bug/whatever comes around. So, this is my admin url:
http://magento.example.local/admin_system/
If I go to this url, I am able to login and then this issue happens. At this point the url is now
http://magento.example.local/index.php/admin_system/[some_irrelevant_stuff]
So this is where it gets odd. If I then navigate to the original url I am once again redirected to the second url but the beginning of [some_irrelevant_stuff] is /index/denied/ (which I assume is relevant now haha).At this point I am provided the header of the Magento admin panel and I am able to logout.
OK so thats one part of the issue. If I then logout, or if I start my login process at /index.php/admin_system/ then I am able to log in and use the admin area as usual. So, the long and short of it is, I don't know what's going on but I'm hoping this extra info might get me more details.
Thanks for your time,
Ryan
Wow, I am genuinely surprised that nobody had an answer for this. Anyway, the following details are not so much an explanation as to what caused this issue, but a solution to stop it happening.
So, as you'll see from the edit of my question, I noticed that this seemed to have a bizarre connection to the URL and using different URLs to access the admin area (eg. /index.php/admin instead of /admin) caused different errrors. So for this reason, and because having index.php in all my admin area URLs, I took a look into rewriting the admin URLs. This is made available using this plugin: http://www.magentocommerce.com/magento-connect/admin-rewrites.html
If I'm not mistaken, I did not need to make any changes to the RewriteRule(s) that are set by default by magento so following the instructions on the plugin page above should work.
Either way, this appears to resolve the issue so I hope it helps someone having similar issues.

CodeIgniter Session syntax

Can someone quickly help me out with CodeIgniter's syntax. I need to access an array I stored in the session's userdata and I cant figure out the proper syntax.
<?php echo $this->session->userdata['user_session']['first_name']; ?>
gives me this error:
Fatal error: Cannot use object of type stdClass as array
All of the answers given in this Question dont work:
Access array variable in session (CodeIgniter)
This is how you get session data:
echo $this->session->userdata('first_name');
Been a while since I've worked in Codeigniter, but if I can remember correctly, when you store an array like you've stated, you'd call it like this:
$this->session->userdata("user_session")['first_name'];
Let me know if that works?
Or you can store that data to a variable, and call the array that way. Like such:
$data = array("bar" => "the_value");
$this->session->set_userdata("foo", $data);
$foo = $this->session->userdata("foo");
echo $foo["bar"]; //Outputs the_value
Let me know if that helped.
However, just to let you know.. Normally, storing the session data goes as follows:
$this->session->set_userdata("first_name", "value");
Really no need to go and set your own array inside of userdata, because that's generally what the userdata array is for.
I found the proper syntax. Well, at least one way to go about it. #Matt GrubB was the closest and put me on the right track.
$temp_session = $this->session->userdata('user_session');
echo $temp_session->first_name;
Since userdata is an object full of info created when I query my database, the easiest way to access the data is to take it and put it in another temporary array. You then have to stab it. I kept stumbling by trying to do $this->temp_session->first_name or something of the like.

Codeigniter Flashdata gone too early

I'll try to make it short.
When i set flash data like this
$this->session->set_flashdata('testing', $somevariable);
I should get something like this in session:
'flash:new:testing' => string 'Test'
And this works.
When I make a new server request it should say
'flash:old:testing' => string 'Test'
I don't get to this part. Even at first request (while it is still "new") i get boolean (false) when trying to get flashdata.
I should mention this is working in wamp, but not on my live site.
Any ideas?
Thanks in advance
It seems strange !
Maybe you should check the value of var_dump($somevariable) in your live site.

javascript-php var post get

That code work:
startSlideshow(<?php echo json_encode(glob("photos-animaux/*.jpg"))?>);
that code dont :
$.post("",{'folder':'animaux'});
startSlideshow(<?php echo json_encode(glob("photos-".$_GET["folder"]."/*.jpg"))?>);
WHY ?, what i am doing wrong ?, help !
why the stupid php fonction just dont make the string right !! ahhhh!
---new infos----
that line work :
startSlideshow(<?php echo json_encode(glob("photos-".$_GET["folder"]."/*.jpg")) ?>);
because if i MANUALLY enter in the address bar ?folder=animaux...bam! work
so the problem shoul be there : $.get("photo-portfolio.php",{folder:"animaux"});
still dont know where !
If you're using $.post() from JQuery, you should use $_POST['folder'] to access your variable. If you use $.get(), then you use $_GET['folder'] in PHP. Try changing that $_GET to $_POST.
Change $_GET["folder"] to $_POST["folder"] ?
You can dump the $_POST to be sure you're getting the right info..
echo '<pre>', print_r( $_POST, 1), '</pre>';
I hope you're not literally writing these two lines together and hope they are interacting, are you?
$.post("",{'folder':'animaux'});
startSlideshow(<?php echo json_encode(glob("photos-".$_GET["folder"]."/*.jpg"))?>);
PHP runs on the server, Javascript in the browser. In the above two lines, if written like this, the PHP is already long done by the time $.post() is called.
PHP processes the code on the server and sends this to the browser:
$.post("",{'folder':'animaux'});
startSlideshow(['something.jpg', 'something2.jpg']);
The browser executes this code:
Post {'folder':animaux'} to "" (no effect whatsoever).
Start a slideshow with ['something.jpg', 'something2.jpg'] (which was already decided by the time the page loaded).
I hope you're aware of this two stage process.

Categories