-1

I have a plans.php page with buy now button. If the user clicks on buy now button then he should be taken to login page. Simple enough.
But when the user reaches the login page a message should be displayed "you have to login to purchase" I dont want to have 2 login pages and this message should be displayed only when the user clicks on buy now button on plans.php page and not otherwise. I am not asking for code just tell me what method is used in php to implement this.

dsdsdlk
  • 7
  • 2
  • "the user reaches the login page a message should be displayed "you have to login to purchase" I " .. but they are on the login page??? –  Dec 15 '18 at 06:16
  • yes, when the user reaches the login page only via plans.php – dsdsdlk Dec 15 '18 at 06:20
  • What you're asking for is too broad and unclear. It would be **beneficial for you** to go over the help area if you haven't already https://stackoverflow.com/help and the related links inside it. Please read through that and you'll see how Stack Overflow works, *"learning the ropes"* as it were. It will give you a good idea on how to formulate a good question, to see what can and should not be asked, as well as what is expected from you. This was made and put into place in order to help you have a better and positive experience here, which is what everybody wants and aims for. – Funk Forty Niner Dec 16 '18 at 04:03
  • You have answers below. Pick and choose which one best answers what you're asking and accepting it in order to close the question. – Funk Forty Niner Dec 16 '18 at 04:04

3 Answers3

2

You can easily do this by using a GET variable in the link when it redirects them,

eg: http://www.example.com/login.php?referrer=buynow

On the login page just run something similar to:

if (isset($_GET['referrer']) && $_GET['referrer'] == 'buynow'){
   echo 'You must login to purchase this product';
}
Second2None
  • 1,470
  • 1
  • 12
  • 22
1

You could use a $_GET variable to store the referring page. But then if anyone bookmarks or shares the resulting link to someone else, then that would cause them to see the same pop up when you don’t want them to.

The $_SESSION variable would work better.

You could also use $_SERVER['HTTP_REFERRER'] to find out where the user came from. It is considered reliable because the value could be faked or empty, but in your case that shouldn’t matter.

Note that, in order to redirect the customer from the login page back to the product page they were just on, you will have to use either $_GET or $_SESSION, or else put the product code into the $_POST variable with your login form, and have the target page do a redirect back to the product.

At some point, you might want to consider a cleaner login experience using a JavaScript Ajax login popup so the user never has to leave the product page.

Buttle Butkus
  • 9,206
  • 13
  • 79
  • 120
  • This answer needs to be updated in order to contain the correct syntax for [superglobals](http://php.net/manual/en/language.variables.superglobals.php). It also contains curly quotes `‘ ’` which will cause a parse error, those should be `'` instead. Also, `HTTP_REFERER` cannot be relied on, as per [How reliable is HTTP_REFERER?](https://stackoverflow.com/q/6023941/1415724). The `$X` will also throw an undefined variable notice. – Funk Forty Niner Dec 15 '18 at 15:25
  • @FunkFortyNiner so you downvoted based on incorrect quotes and because HTTP_REFERRER is unreliable, which I mentioned? He is not using it for security sensitive purposes, but just to display a message. – Buttle Butkus Dec 16 '18 at 02:51
  • Yes, least I'll say something about it. How many times people complain that nobody ever says anything about that? What did you expect me to do? Let's hear it, I'm all ears (no sarcasm intended). Btw, if you were so much in a hurry to submit this, surely you might have had a good reason, right? Typed on a phone maybe? - Edit: Btw, I removed my downvote. Thanks for fixing it. – Funk Forty Niner Dec 16 '18 at 03:24
  • @FunkFortyNiner it is pretty cumbersome to pick out the right quotes on my iPhone. I got the backticks right for code. I guess I picked the wrong quotes. Also, since I don’t use superglobals that much I guess I copied the format in the other answers without realizing it was wrong. Anyway, those issues are fixed now. – Buttle Butkus Dec 16 '18 at 03:28
  • 1
    ah, that's what I thought which is why I edited my comment about that before you're saying it. Well if I may offer a suggestion. I see other people submitting answers on their phones *but* they leave a note that they typed it on one and that they'd come back and fix it later. In doing that, it helps others to know what's really going on and gives them the right time of day, as it were. So in a way, sorry if I didn't know, but that tip I'm giving you might help you later on. Just trying to help. – Funk Forty Niner Dec 16 '18 at 03:31
  • I don't mean to tie up comments or to carry on, but about your *"He is not using it for security sensitive purposes"* earlier. What the OP states *"should be displayed only when the user clicks on buy now button on plans"* - sounds to me like there is some form of security that should be involved here. There could be anything going on that they're not telling us. That could be anything. Looks to me like they're wanting to sell online, so security *is* an issue. But since they didn't tell us about it, then it's up to them to suffer the possible consequences. – Funk Forty Niner Dec 16 '18 at 03:38
  • @FunkFortyNiner security is involved. But the direct use he is making of it is just to display a message. Of course he should not use it in any way that would depend on it for access to privileged areas, etc. I am guessing he is working on a school project or just “fooling around” with programming. So I gave him another option to play with and then told him what I think he should do if he’s really serious. I might add more detail later. – Buttle Butkus Dec 16 '18 at 03:48
  • Yeah, they have a bit to work with. We'll see what kind of feedback everyone gets, given a big "IF" they will, who knows. Oh well, *"c'est la vie"* as it were. *Cheers* – Funk Forty Niner Dec 16 '18 at 04:02
0

use $_SESSION. set your message on a session like this: $_SESSION["logout_msg"]="you are not login"; and when redirecting on login page use this code

if (($_SESSION['logout_msg'] != '') && (isset($_SESSION['logout_msg']))
{
    echo $_SESSION['logout_msg'];
}
mahradbt
  • 364
  • 1
  • 2
  • 12