i have a issue with my SESSIONS, on the first page the user clicks on a anchor, then if the user is logged in will be redirected without problem, but if not, first he will pass through login page, but when the user reach thelogin page, the variables are missing. (The login page occurs after the redirect page). So i have saved the SESSIONS with $_GET parameters.
How i can keep the current SESSIONS to redirect the user after the login?
Thanks!
EDITED
Page: retailer.php (this page is where its fired the parameters)
<a href="/go2store.php?rid=<?php echo $row['slug_title']; ?>&c=<?php echo $row_coupons['coupon_id']; ?>" onclick="javascript:window.location.href='<?php echo $row_coupons['logout_url']; ?>';" target="_blank">
Page: go2store.php (this page is where i'm saving the SESSION)
$_SESSION["myCupon"] = "/go2store.php?rid=".$_GET['rid']."&c=".$_GET['c'];
Page: redirect.php (This page its checking if the user is logged in, if yes, redirect to the URL on the SESSION, if not will send to login.php)
Page: login.php (here i have this on top of the code)
session_start();
$_SESSION["myCupon"] = "/go2store.php?rid=".$_POST['rid']."&c=".$_POST['c'];
But it comes empty, its not returning any values after the redirect.php page.
As noted by a user in the comments, you need to include:
session_start();
At the top of each page to carry session variables across pages, otherwise it won't recognise the vars you set initially.
We don't know how you're organising your code, but this needs to be present both before and after potential login.
You can easily set/get $_SESSION variables prior to login or indeed set/get upon login from other vars you might use in pages that aren't yet authenticated.
Related
I'm trying to create a Session-Based flash message in PHP:
In register.php page, I set the session as follow:
$_SESSION['flash'] = 'Registered';
Then, after redirecting user to the home page, I printed the flash message:
if (isset($_SESSION['flash'])) {
echo $_SESSION['flash'];
unset($_SESSION['flash']);
}
The session is started in both pages.
The problem is:
I get the flash message in the home page only if I remove the unset function, and then the message is always printed.
I wrote a library just for this type of issues: https://github.com/tamtamchik/simple-flash.
Once you have it installed you can do this:
// put message not session
flash('Some error message', 'error');
// print it after redirect
echo flash()->display();
It'll generate Bootstrap friendly alert messages.
I just solved my problem by adding exit after redirecting user to escape the execution of the register page, so the session won't be unset in the current page before using it in the next page.
Please note that session_unset just clears out the sesison for usage. The session is still on the users computer. Note that by using session_unset, the variable still exists.
Using session_unset in tandem with session_destroy however, is a much more effective means of actually clearing out data. As stated in the example above, this works very well, cross browser:
<?php
session_unset();
session_destroy();
?>
I have basic login code system that every values of the textboxes(user inputs) will store in a SESSIONS. After the user's login, my HOME.php showed. And in a HOME.php, The user clicked the back arrow of google chrome, and it seemed the page is going back to the INDEX.php where user logged-in. I was trying if the sessions in index.php are exist. if not go back to home.php?
How could I prevent if the user try to click the back arrow of chrome and still remain in home.php?,
You should check whether the session has set in the index.php and if it was set then you should redirect the user to home.php else to remain same in the index.php
So, In your index.php you should have this code
if(isset($_SESSION['user']))
{
header("Location: home.php");
}
else
{
#Ask for credentials using your form
}
Note : I am using $_SESSION['user'] you should the name that you are using.
I am not exactly sure what the problem is, but I think you want to check if the user session exists in your index.php page. If it does, redirect automatically to home.php and if it does not, show the page contents / login form.
I have two iframes named "header" and "content". When the index page is loaded, the "header" contains a link to the Home Page, and another link to the Login Page. While the "context" displays stuff about the page and all that.
Now the problem is, after I log in, I want both of the frames to reload. I can change "context" because this is the iframe where I will do the logging in so there's no problem.
What I am tyring to do now is to find out how I could also change the "header" iframe. After logging in, I want it to display a link for Home Page for his tasks, and a link that would display the username of the logged in user for his profile.
Is there any way I could do this? I am using PHP, by the way.
Thank you in advance. :)
it would be better if you share your codes with us, its hard to answer by this way, you can try using php sessions for check a user logged in or not. after the code which you checking login, if login is success create the session and define $_SESSION['userName'] or userID as you wish and you check if session userName set on your header or content.
<?php if($_SESSION['userName']): ?>
// your html codes goes here for logged in user
<?php else: ?>
// your html codes goes here for non-user
<?php endif; ?>
On a password protected website (ajax-php-jquery login system), a fadeIn/fadeOut alert confirms that the user just logged out. The user is sent back to the login page and I do a document.referrer to check if he comes from a page of the site (if so it means that he just logged out so I can display the message). But of course if the user refreshes the page he will get the notice again.
I was thinking of changing the document.referrer but the property is read-only. Any idea how I could display the message only once if the user did log out?
You could do a conditional with PHP:
if(isset($_SESSION['justLoggedOut'])){
print '<script type="text/javascript"> ..notification code.. </script>';
unset($_SESSION['justLoggedOut']);
}
You just have to set the session from within the authenticated area (or the logout page which redirects to the index page [I'm assuming.])
Using a cookie would be easiest route. Simply add a cookie when they log out, and then delete it after the notification has been shown. This cookie could contain all of the information you want about the log out: e.g. the time the logged out and the URL from which they initiated the log out.
Of course, that is assuming your clients are allowing cookies on your site. A server side alternative would be to use session data. Your notification would then send an AJAX call back to the server to clear the indicator that the notification should be shown.
In my JSP page I am using post method while submitting the page.
So once I go from Page 1 to page 2. In Page 2, If I press F5 I am getting alert as
"To display this page, Firefox must send information that will repeat any action (such as a search or order confirmation) that was performed earlier."
I knew this question is a bit sarcastic but please give me an Idea.
I can't change my method from POST to GET because I need to send large amount of data.
Thanks in advance...
Edited:
In my Page1.JSP I call onClick function in that function I call action as "/page2servlet.do".
Now, In Java side I use Spring Framework. With MVC Object I return to page2.jsp.
So where do the response.sendRedirect Fit.
Do a redirect to Page 2 after POST. You will not receive the prompt anymore.
After doing POST save all the info you will need to session and send an instant redirect to other page with GET. Then on that other page get all the info you need from session.
However, after the session expires user wont be able to press Refresh. Also, it will break multi-windowing. User won't be able to do 2 distinct submits in separate windows as they will share same session object.
<?php
session_start();
if(!isset($_SESSION['disablethispage'])){
$_SESSION['disablethispage'] = true;
// serving the page first time
}else{
// visited before or page was refreshed
}
?>
Here is a version that replaces the next page:
http://plungjan.name/testredirect.html ->
redirect.php ->
thanks.html
and here is redirect.php
<meta http-equiv="refresh" content="3; url=http://plungjan.name/test/thanks.html">
<script>
location.replace("http://plungjan.name/test/thanks.html");
</script>
redirecting..
.