In a PHP script I work with $_SESSION. For 95% of the website visitors, the script works as desired. Unfortunately, it does not work for the remaining 5%. What could be the reason for this?
I meant that the website visitor (not like cookies) has no influence on this.
Related
Maybe my understanding of a session is off, but isn't a session stored in the browser? More like, as long as the browser is open, the session should be active, right? (If you don't change the default behaviour).
We have multiple languages in our project, which we are trying to get rid off. Currently, 90% of our code is running in classic asp and will be replaced with PHP.
If I try to access my variables in the asp-part, it works fine:
<%= session("name") %>
This outputs "Jon" for instance.
If I try to get this output via PHP like so:
<? var_dump($_SESSION); ?>
i simply get NULL. Needlessly to say, that <?= $_SESSION["name"] ?> doesn't work as well, then.
I'm never leaving the site, only the paths are different:
mysite.com/default.asp works fine
mysite.com/phptest/session.php only returns NULL when trying to access the session. Why is that? Both languages are running on an IIS. Do I have to somehow tell PHP to access the existing session?
I'm fairly certain, that I combined accessing Session-Vars in PHP + JavaScript before and that worked fine.
The session object is specific to Active Server Pages. If you have a mixed site that runs both PHP and ASP, you won't be able to use ASP session variables in your PHP pages. The objects and variables in Session are managed by ASP; the only thing the browser is involved with is keeping a reference to that session in a cookie.
What you could do is save the content of the session variables to a cookie, which could then be read by your PHP pages (with a little twist, apparently: Read classic ASP's cookies with PHP )
Hope this helps!
Apologies in advance for the lengthy post, just trying to explain the situation clearly.
I've created a PHP-driven website for searching a big (millions of records) MySQL database of people. On the search page you have your usual form for search criteria. Due to the way the people often use the site, the search criteria are saved into session variables so that if the search page is reloaded the previous criteria remain in the form fields (there's a button to manually reset the criteria, of course). This in itself works fine.
I also have two language selection links that store the language selection in a session variable (making the page header load an appropriate localization file), and as above, this in itself also works fine.
What's problematic is that when a user gets the search result, a list of people, and wants to open up detailed info on a person (thus going from search.php to info.php) and then wants to go back to the people listing via the back button, it takes too long to reload the previous page as the page re-sends the MySQL query etc, instead of going back to a cached page. It can take even 5 seconds or more sometimes as the queries produce up to 5000 results - but even say, 200-500 results takes long to reload because the database itself is big and not the fastest in the world. And limiting the number of results isn't really practical.
The obvious solution at first glance would SEEM to be enabling the browser cache. Which is exactly what I did via PHP header and pragma controls. And all seemed well, as going back to the list was basically instantaneous. However, I realized that enabling the cache means the updated session variables don't work. New search criteria doesn't properly replace the old ones when reloading the search page after having been to a different page, and even though you select another language, pages open up in the language you previously were using, because that's the way they were cached! You can force the language to update via F5, but that doesn't seem to help the search criteria much. But even if it did, F5-spam isn't really an answer, it needs to work automatically.
So long story short, how do I make the search result list open quickly without making session variables useless? Or will I simply have to make do with sluggish page loads when using back button, thus annoying users? I really don't want to open the info.php in a new page, either.
Have you considered caching the database results on the file system? I have found the Zend Framework caching class to be very easy to use. You can use any information you want, to differentiate cached results from other cached results. So the caching can be as fine grained as required.
http://framework.zend.com/manual/1.12/en/zend.cache.introduction.html
You don't need to use the whole of Zend Framework to use the class. You can use it on its own.
I wonder if the following would be a good Idea or rather contra-productive performance-wise:
An Ajax-Application, like for example a pagebrowser needs some language- and config-values, which are stored in the database. So each time, the user is using this app, in the ajax-script the mysql-query to get the variables is done again and again. concidering this for a pagebrowser, there might be like 10 or more requests (back and forward, back, forward, and so on), aka 10 x database-select, while it is needed only one time actually.
My idea was, to safe the config-vars in a session-array the first time, the ajax-app is requested. If the sessions-array exists, the mysql-query isnt done again.
if the user calls another regular page, these session-array is deleted again.
Now im not really sure, what would consume more server-resources, using sessions in the above described way for saving the vars teporarily, or just using a mysql-query to get the vars each time, the user klicks the ajax-app.
Thanx in advance, Jayden
If you working with massive amount of data, you could consider using Cookies as well instead of session for server resources, which will be stored in the user's local browser.
I'd bet sessions would be more effective, but the best way is to test and measure the different execution times.
I am using a simple bit of PHP code in my header file which increments a counter in my SQL database by 1 once per PHP session. I've tested this and it works fine.
However when I leave it for a day the counter has gone up by way more than I believe it should, and comparing this to the pageview counter in my Google Analytics it is far too high.
What could be happening and how could I stop this?
Google-analytics has a very different way of counting visits than a simple session based counter. I can't tell you exactly how it counts it because it is very closed source on that aspect but there is definitely cookies, sessions and javascript involved.
If you want my opinion. I built my own stat system once and it was hell with all those robots detection, trends, false visits. I switched to GA and it was worse because the client then started complaining that the numbers werent the same in both sites.
IMO? Don't use both, make up your own or use GA only, but not the two, you'll probably NEVER hit the same numbers.
Good luck
What do you mean by "once per session"?
You need to do a start_session() and then set a variable to ensure you are only counting unique sessions:
if(!isset($_SESSION['started'])) {
doHitCounter();
$_SESSION['started']=true;
}
I have a social network type site (member site) I store a lot of user data like username, userID number, email, first name, photo URL, into a user session when the user logs in to cut down on the ammount of DB queries per page. I sometimes hear people talk about using cookies for some things, I realize cookies should never be used for username/passwords but mu main question is, does a cookie slow down the page loading time? On a user's homepage I have blocks that a user can drag and drop to re-arrange there homepage, my first version I stored the location of the blocks in a cookie, I now store this value in mysql and I build the locations of the block in php on page load instead of using javascript to set the initial locations. When I first used cookies to set there locations though the page would load and the blocks would be in there original location, the page would then really fast change the location of these blocks from the value it got from the cookie, however this was much much slower and made the page seem really slow to load. I have always thought cookies were slow since then but maybe it wasn't the cookie that slowed it down.
So do cookies slow a page down?
A cookie is just an extra HTTP header with key/value pairs. Parsing that header and filling the $_COOKIE array does not add significant overhead (it's almost certainly not going to be a bottleneck).
If you're storing a lot of data in the cookie (i.e., not just a session id),then it's worth remembering that this data gets sent with every request to your domain, not just for PHP pages but for images, CSS, JS etc. For this reason, in a high traffic site you might arrange for these "static" elements to be served from a different domain to lessen the effect of this overhead.
If that cookie value is a session identifier, then PHP needs to retrieve that session, either from the filesystem, database or other storage mechanism to populate the $_SESSION array. This can take add a little time, but it really depends on the mechanism used.
If I understand correctly, you are saying your page was slow when :
the positions of the blocks were stored in cookies
and the blocks were positionned in Javascript on page load
I think the "slow" part doesn't come from the cookies :
those are quite small, compared to the size of the page
yes, it adds some overhead, but not that important for a single page
it becomes more important if you have lots of assets (images, static files, ...), as they are sent with each request, though
they don't contain much data
But, with that solution, what could cause an impression of slowness is that :
the page is loaded with block at their default positions
some JS code is executed to re-position the blocks
which means waiting a bit before blocks are re-positionned
which takes some processing time to execute the JS code
which also means, for the browser, re-drawing the page several times (each time something changes).
IMHO, what makes your page "faster" now is that the client receives the whole page with blocks already well positionned -- which means no processing at all on page load :-)
The smaller/absent cookies might make a small difference... but small.
If you want to read more about cookies size and their impact on load-times, and some recommandations, you can read this article from Yahoo : Performance Research, Part 3: When the Cookie Crumbles ; it's a pretty interesting read, and not too hard to understand.
Cookies will slightly increase the load time for your pages, as the cookie data has to be sent with each request.
For the HTML page, this won't make much difference, but if you have all your assets on the same domain (as is usual) it can add up to a significant difference.
Actually reading the cookie in PHP should not take a noticeable amount of time.