Page 1 of 1

Store and redirect to "Last" referral link

Posted: Mon Jul 22, 2013 6:14 am
by lking1221
Hello Everyone,

I need help in storing the "Last/previous" referral link that user navigated from. The reason is because I need user to login before viewing the enquiry page. So most of the case if user is not logged in / guest, they will be redirected to Login page first and once they logged in, we will redirect them back to enquiry page (based on referral link). In general, these processes are all working properly only when user is successfully login with correct username and password in the "First time" BUT not when the user entered the Incorrect username and password (as the referral link updates and become as the login link because pointing back to login form). So may I know how do we store the referral link as in static variable value and will not change even if user is entering the incorrect username and password and being redirected back to the login form? I tried session, but it seems like not working (referral link still updates) or maybe I'm doing it wrongly. Please advice how do we achieve this solution.

The process as follow,
> Product page (once click on enquire button)
> Enquiry Page check if logged in, if yes > Show Enquiry Page, if no > go to Login page
> If login successful, go to > Enquiry Page, if failed stay in Login Page

For example:
1. Referral link is:
http://www.domaintest.com/products/antova123

2. Enquiry Page is: (Check if logged in, if yes show page, if no go to login page)
http://www.domaintest.com/enquiry

3. Login Page is: (The code should be in this page) - If failed, back to this page. If passed, go to Enquiry page based on referral link.
http://www.domaintest.com/login

Thanks alot,

Regards,
Josh

Re: Store and redirect to "Last" referral link

Posted: Mon Jul 22, 2013 9:45 am
by AbraCadaver
However you set your referral link in a session var, just don't set it if the page is login.php.

Code: Select all

if(strpos($_SERVER['SCRIPT_NAME'], 'login.php') === false) {
   //set the session var with referral link
}

Re: Store and redirect to "Last" referral link

Posted: Mon Jul 22, 2013 3:14 pm
by lking1221
Hello AbraCadaver,

I still not understand how do we get this codes work. Just say my login page is http://www.domaintest.com/login and (the uncertain referral link) is http://www.domaintest.com/products/antova456 , how do we stop referral link to update if user login is unsuccessful and redirected back to login form? I did some echo test and the following are the results:

First attempt to login form:
Current url:
http://www.domaintest.com/login
Referral url:
http://www.domaintest.com/products/anto ... 3&item=1...

So if first login is successful, everything is fine, user will be redirected to http://www.domaintest.com/enquiry/antov ... &item=1....
However, if login is unsuccessful, the Referral url updates and becomes http://www.domaintest.com/login (how do we stop it from updating?)

Thanks alot for helping

Please advice,

Regards,
Josh

Re: Store and redirect to "Last" referral link

Posted: Mon Jul 22, 2013 4:46 pm
by AbraCadaver
What code are you using to set the referral link and do you put it on every page or in an include file?

Re: Store and redirect to "Last" referral link

Posted: Mon Jul 22, 2013 6:27 pm
by lking1221
Hello,

I'm using this code in a CMS only 1 page (the login form). The following is the code as sample:

<Script load when form is loading>
.... Ingore login form details on top ....
//Current URL - To display the current page url (Working properly)
$url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
echo $url."<br />";

//Referral URL - It shows for example "http://www.domaintest.com/products/anto&item=1&param4" initially, but change to "http://www.domaintest.com/login" if login unsucessful:
$urla=$_SERVER['HTTP_REFERER'];
echo $urla;

<Script load when form is submitting>
-----------------Ignore login data and codes until the following lines---------------------
if (true === $app->login($data, $options)){
header('Location: [how do we use referral url here?]'); <<<< My question is how to make the header location as the last/previous referral link as i.e:"http://www.domaintest.com/products/anto&item=1&param4", even if the login was unsuccessful at the first few times.
}

Please advice,

Thanks

Regards,
Josh

Re: Store and redirect to "Last" referral link

Posted: Tue Jul 23, 2013 9:20 am
by AbraCadaver
Well, to use it on the login page, this may work. However $_SERVER['HTTP_REFERER'] is not always reliable as it may be changed by the user agent or proxy server.

Code: Select all

if(strpos($_SERVER['HTTP_REFERER'], 'login.php') === false) {
    $url = "http://".$_SERVER['HTTP_REFERER'];
}
I would probably include this in the header that is included on every page:

Code: Select all

if(strpos($_SERVER['REQUEST_URI'], 'login.php') === false) {
    $url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
}

Code: Select all

//later
header("Location: $url");