Here's my issue.
I have a site where my users have a page setup for them that displays their information (basically a public profile page) Ex. example.com/johnsmith, example.com/janesmith
When you visit example.com/johnsmith, I used PHP to create a cookie that stores some of John Smiths information, so that when pages outside of John Smith's are being viewed his information will persist across the entire website (example in a banner at the top of the website), so that users can quickly navigate back to John's page.
However, if someone visits example.com/johnsmith and then goes to example.com/janesmith and then clicks back to say the home page, John's information will still display despite the cookie being overwritten with Jane's information until you manually refresh the home page, at which point Jane's information displays correctly.
Any ideas as to what the issue could be causing the overwritten cookie to not display information on other pages until a manual refresh is performed
Related
Every page on my site does a login check. If the user is not logged in, they are redirected to the login page. But after they log in, if they want to go to that page they were redirected from it is missing from the browser history. So...
I'm on Page A
I click to Page B. Oops! My session timed out and I'm not logged in.
I'm redirected to Login Page (using PHP header( 'Location:http://example.com/login.php'); function). I log in.
If I hit "Back", I go to Page A. Page B is NOT in history.
How can I make it so hitting the Back button takes me to Page B?
EDIT for clarification: Back takes me to login page again. Back a second time takes me to Page A.
You could have the login page redirect the user to the page which they were previously browsing. Basically, during your authentication function, you would grab the current URL or query string and save it for after a successful login.
There are a few different methods of storing this, either
Cookies (see documentation for setcookie() here)
Session (see documentation for $_SESSION here)
In the URL, by encoding the previous URL as a variable and passing so you would redirect to http://example.com/login.php?redirect=somepage.php
Once the login is successful, you would load that page rather than the general home page.
However, this doesn't specifically address the question you asked. In terms of altering the browser history, as was mentioned in the comments, you would need to use Javascript, and the History API documentation is here.
We want to achieve a functionality where a user goes to a domain A, puts stuff in their basket. When they go to domain B (also owned by us) the contents of the basket he made in A shows up here too.
How do you achieve this in php? How does site b know the identifier of his session on site a?
We looked at coolblue.be and gsmstore.be for inspiration but are clueless :D
You need to create system which does this:
1.When a user logs in on page A you need to set the session id for him and redirect him to B with this session id.
1.1.Session data needs to be stored in database(sessions can be stored in cookie or database, you will need to set saving in a database).
2.When a user is redirected to B this page gives him another session id.
2.1.you need a table in database for page B with sessions_id from pages A and B
2.2.user is then redirected to page A.
3.The user clicks trough page A and add products to basket
4.After some time user come to page B.
4.1.On first visit app checks if user is checked if he have session id from page a from database table for sessions_id from pages A and B.
4.2.If the user haven't visit page A nothing happend, but if user have session from page A, app gets basked data from page A database for sessions and saves to user session in page B. On page B. In both cases in session is set status that user is checked becase we don't want check on ever pageview
5.DONE!
Addition.
1.Records in database table for sessions_id from pages A and B older than few hours can be deleted.
2.The same thing for page A need to be done for page B in same way.
I want to show different home page when user visit every time my site, means new html page on every new visit or repeat visit.
When user open a site the first time he/she see x.html next time y.html next z.html and so on.
Please help me
You may use cookies to store information on user (client) side that he/she has already visited your site. Please refer to: http://php.net/manual/en/features.cookies.php for info about cookies in PHP. It requires that user accepts cookies in the browser. In the cookie you may keep info about already seen pages.
Yet another way is to store such information on server side but it may be risky because it is not obvious how to identify unique user on server side (combination of IP + browser - may not always work - users from the same private network may provide the same externally visible IP).
The last solution that comes to my mind is to force user to login to your page. Then, upon login, you may count a number of times the given user (identified by username) has been on your page and provide diffent page each time.
Create a script that will choose a different page at random.
Store the pages in an array
$pages = array('page1.php','page2.php','page3.php');
Get random number
$rand = mt_rand(0,2);
Use that random number to choose page from array
$rand_page = $pages[$rand];
Then show the page to the user
include($rand_page);
I am new to PHP, but I created a website with a login, register, and personalized homepage for different users.
I have three users and three tabs in my web browser with the sessions open. Sometimes, if I click on go back to home in say user1, the button would return me to user2's home page.
What do you think it is wrong?
Thank you.
It sounds like you are trying to simulate all three users yourself, just using tabs in your browser. Then you are going to have collisions -- whatever tab was loaded last will set a session cookie, overwriting the other tabs' cookies. All tabs will then be effectively using that last tab's session.
Try testing it with two different browsers -- you'll see they don't conflict, because the browsers don't share their cookies.
I am just starting to learn about web development and something has been niggling me for a while now, How a website controls what you can access and cannot access.
For example, a website like Facebook. When i first go to the site, it presents a login form, once i am logged the same page that i tried to access before now shows information relevant to me that i could only access once logged in, i can navigate to a different site and then comeback to google and it still allows me to use if without logging on again.
How exactly would a site block someone trying to access a particular page when they are not logged in, lets say the page viewProfile.php. How does the website know who to allow access to this page?
I realise this question may seem confusing and elementary but its just a something that came to me whilst viewing facebook.
Thanks.
This is a very simple concept called sessions.
When you visit facebook, it reads unique information sent to it via the connection such as IP address, browser, and some other minor information, when this information is combined it creates a unique identifier.
this unique identifier is then stored in a file like so:
d131dd02c5e6eec4693d9a0698aff95c.session
So when you login with your credentials there application add's information into this file such as last activity etc.
When you go away and come back, facebook will then read the information that's sent with every requests, it then add's it all together and creates a unique hash, if this hash exists within it's storage system it will open it up and read the contents, and know exactly who you are.
all this is combined with cookies, the unique hash is sent back to the browser and stored in your cookies folder, this cookie file is sent back to facebook with every request.
PHP Handles this for you internally so it's pretty basic to get it up and running: http://php.net/manual/en/features.sessions.php
Here's an example that may help you understand the concept a little more.
<?php
/*
* The session_start generates that hash and send a cookie to the browser
* This has to be first as you can only send cookie information before any content
*/
session_start();
/*
* Anything storeg within $_SESSION is what's been read from the session file and
* We check to see if the information has already been set on the first time the user
* visited the site
*/
if(!isset($_SESSION['hits']))
{
$_SESSION['hits'] = 0;
}
/*
* Now we increment the value every time the page is laoded
*/
$_SESSION['hits']++;
/*
* now we display the amount's of hits the user has loaded the page.
*/
echo 'You have vistited this site <strong>' . $_SESSION['hits'] . '</strong> times.';
?>
if you load this page and then hit F5, the session value get's incremented every request so you should see something like:
You have vistited this site 1 times.
You have vistited this site 2 times.
You have vistited this site 3 times.
You have vistited this site 4 times.
...
The session file is unique to each person visiting, thus meaning that when using the session variable in PHP it would be to that user only, so everyone get's there own individual session.
as your researching it's goods to search StackOverflow for certain tags, such as PHP and sessions.
https://stackoverflow.com/questions/tagged/php+session
Here's a good question in regards to cookies and sessions advantages etc.
Purpose Of PHP Sessions and Cookies and Their Differences
A website uses something called a "cookie" to store information on your computer.
This information can hold any text string, but in this case it is probably a unique ID that Facebook knows (probably stored in a database somewhere) is tied to a certain user. Cookies can only be read by the website that sent them and by the browser itself.
The login page sends a POST/GET request to a script that generally checks the username/password combo against data in a database a database. If the data is found to be valid, then the user is granted access to the websites landing page (the page after login) and a cookie is stored. If it is not, they are sent back with a error message.
Cookies can also have a "lifespan". This lifespan can be anything: for a certain amount of seconds; until you leave the site; until you close your browser; or forever (there are probably more.)
The website that sent a cookie can also delete a cookie before it expires. This is how most "logout" buttons work.
To allow only logged in users to view content you can first check for a sign that they are logged in, such as look for an active session and that it has a flag which tells you they're logged in ( which you control ). In PHP at the top of a page you can simply:
<?php session_start();
if(!isset($_SESSION['loggedin'])){
header('Location: http://example.com/login.php');
}
?>
which will redirect non logged in users to a login page. Upon success login, you should set $_SESSION['loggedin'] to a value.
To check whether a person who is logged in is allowed view a particular profile is down to looking up where the page is restricted to friends only, and if so, checking that the loggedin user's id is in the profile owner's friend field in the DB.
It is done with cookies. When you log in, the site puts a cookie into your browser for a set amount of time (generally a very long time so that you can stayed logged in). When you access the site again, your browser sends the cookie back to the site (and the site sets a fresh cookie). In any browser, you can find the list of cookies somewhere in the options.
If you want to know more about cookies, you can read the wikipedia: http://en.wikipedia.org/wiki/HTTP_cookie
Do a Google search for "Session Management."
Summary
when you login to a site you get a unique id. That id pulls your data from the database and then populates a dynamic page, like viewProfile.php with your data. So each user pulls the same file, viewProfile.php, but gets different results based on their unique id.