Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

This should be easy: Detect login status using JavaScript?


Mugsy

Recommended Posts

Posted

I've created a custom front-end for my OSC site and I need to detect if the user is logged in or not (preferably using JavaScript).

 

OSC creates two cookies: one named "osCsid" and another called "cookie_test" ("osCsid" seems to come & go for no apparent reason, not connected to your login status). So far, all my attempts to read either cookie have failed.

 

Or maybe someone knows another way to detect login status?

 

Can anyone help? This should be an easy one, but for some reason, I'm getting nothing. :blink:

Posted
I've created a custom front-end for my OSC site and I need to detect if the user is logged in or not (preferably using JavaScript).

 

OSC creates two cookies: one named "osCsid" and another called "cookie_test" ("osCsid" seems to come & go for no apparent reason, not connected to your login status). So far, all my attempts to read either cookie have failed.

 

Or maybe someone knows another way to detect login status?

 

Can anyone help? This should be an easy one, but for some reason, I'm getting nothing. :blink:

 

 

When you are logged in OS commerce sets a hashed session for you when you are logged in - you'll need to use that - I personally would not use javascript - there are functions already declared for that in OSC. You should investigate that road instead of trying to javascript it out - plus, jscripting may be blocked for other people, not activated, etc.

Nothing unreal exists

Posted
OSC creates two cookies: one named "osCsid" and another called "cookie_test" ("osCsid" seems to come & go for no apparent reason, not connected to your login status). So far, all my attempts to read either cookie have failed.

there is a good reason for the oscsid showing with the address bar. The osc has to send it if it cannot send the cookie basically. (domain switch etc).

 

To detect if the customer is logged in you do:

 

// if the customer is not logged on, do something
 if (!tep_session_is_registered('customer_id')) {
// Do something here
 } else {
// Customer is logged on do something else
 }

 

and you don't do jscripts as already mentioned.

Posted
When you are logged in OS commerce sets a hashed session

Doesn't "hashed session" mean it's hidden? It doesn't need to be JavaScript. I *could* use PHP, though OSC's own login detection code is too dependent on other OSC files to reuse in my own page, and I'm not sure how to recode it from scratch. Jscript would just be easier (I'm already detecting if cookies are enabled using JS).

 

Any method for detecting Login Status would be helpful. Thanks.

Posted
Doesn't "hashed session" mean it's hidden? It doesn't need to be JavaScript. I *could* use PHP, though OSC's own login detection code is too dependent on other OSC files to reuse in my own page, and I'm not sure how to recode it from scratch. Jscript would just be easier (I'm already detecting if cookies are enabled using JS).

 

Any method for detecting Login Status would be helpful. Thanks.

 

 

Sorry - I mean encrypted - they code the session so it's harder to hijack.

 

enigma1 has the right answer I believe.

Nothing unreal exists

Posted
Doesn't "hashed session" mean it's hidden? It doesn't need to be JavaScript. I *could* use PHP, though OSC's own login detection code is too dependent on other OSC files to reuse in my own page, and I'm not sure how to recode it from scratch. Jscript would just be easier (I'm already detecting if cookies are enabled using JS).

 

Any method for detecting Login Status would be helpful. Thanks.

you cannot rely on client-side scripts to detect sessions. Unless you're looking for trouble. You should modify your custom front end be part of the osc framework. Because now you modify static html files separately? while you could use osc and edit the page content from the osc admin and utilize the dbase and use couple of scripts only.

Posted
To detect if the customer is logged in you do:

 

// if the customer is not logged on, do something
 if (!tep_session_is_registered('customer_id')) {
// Do something here
 } else {
// Customer is logged on do something else
 }

Thanks. I'm working to incorporate this now (wish me luck). Is there a way to retrieve the Username this way?

Posted

whatever is in the database you can retrieve it at any time, for example with the customer details see how the login.php works.

 

Here is the query

$check_customer_query = tep_db_query("select customers_id, customers_firstname, customers_password, customers_email_address, customers_default_address_id from " . TABLE_CUSTOMERS . " where customers_email_address = '" . tep_db_input($email_address) . "'");

 

which compare with the email address entered. If you already have the customer logged in you could use the customer_id

 

	$customer_query = tep_db_query("select customers_firstname, customers_lastname, customers_email_address from " . TABLE_CUSTOMERS . " where customers_id = '" . (int)$customer_id . "'");
$customer_array = tep_db_fetch_array($customer_query);

so now you have all these fields ready for further processing

$customer_array['customers_firstname']

$customer_array['customers_lastname']

$customer_array['customers_email_address']

 

So in the same way you can retrieve other info from the dbase from whatever table.

Posted
whatever is in the database you can retrieve it at any time, for example with the customer details see how the login.php works.

Crap, It seems I'm already in over my head. Are there any "Includes" I should be using? Since all these queries are referencing functions in php scripts not in my root directory, I've spending all my time leaping from one error to the next and making very little headway in the progress. :'(

 

It appears my PHP experience isn't close to where it needs to be for me to make this work, so any help you can provide would be much appreciated. Thanks.

Posted
Crap, It seems I'm already in over my head. Are there any "Includes" I should be using? Since all these queries are referencing functions in php scripts not in my root directory, I've spending all my time leaping from one error to the next and making very little headway in the progress. :'(

 

It appears my PHP experience isn't close to where it needs to be for me to make this work, so any help you can provide would be much appreciated. Thanks.

 

 

You need to post errors, so we can help you figure out what's going on.

Nothing unreal exists

Posted
Crap, It seems I'm already in over my head. Are there any "Includes" I should be using? Since all these queries are referencing functions in php scripts not in my root directory, I've spending all my time leaping from one error to the next and making very little headway in the progress. :'(

 

It appears my PHP experience isn't close to where it needs to be for me to make this work, so any help you can provide would be much appreciated. Thanks.

 

Ok to make it easy copy one of the oscommerce files lets say privacy.php into the root of your domain. If you simply try to access it, it won't work because of the path difference. But you can make it work easily by changing the privacy.php at the root:

 

right before this line in privacy.php

require('includes/application_top.php');

 

you add:

chdir('catalog');

 

I assume the osc folder is called catalog. And there you have it. So it's very simple to have the entire web-site using the oscommerce core. And you can use the sts and one of the seo urls contributions to make it look like plain html (or xml or whatever). You can use osc for your personal website with little effort.

Posted
Ok to make it easy copy one of the oscommerce files lets say privacy.php into the root of your domain. If you simply try to access it, it won't work because of the path difference. But you can make it work easily by changing the privacy.php at the root:

Dude, you're a God! Thanks a ton!

Posted
To detect if the customer is logged in you do:

 

// if the customer is not logged on, do something
 if (!tep_session_is_registered('customer_id')) {
// Do something here
 } else {
// Customer is logged on do something else
 }

 

Hate to bug you again, but this query does not seem to work. Even after I log in, "!tep_session_is_registered('customer_id')" is still True. Here is my code (inside my "myindex.php" file in the Root):

 

(...)
<td align="right"><?php if (!tep_session_is_registered('customer_id')) echo '<a href="http://www.fly-n-high-discgolf.com/oscommerce/catalog/login.php">Please log in.</a>'; else echo 'Logged in. Welcome back!'; ?></td>
(...)

 

It seems to work, but if I log in and click the link back home, it still says "Please log in.", meaning the condition is still True. Any ideas?

Posted
Hate to bug you again, but this query does not seem to work. Even after I log in, "!tep_session_is_registered('customer_id')" is still True. Here is my code (inside my "myindex.php" file in the Root):

 

(...)
<td align="right"><?php if (!tep_session_is_registered('customer_id')) echo '<a href="http://www.fly-n-high-discgolf.com/oscommerce/catalog/login.php">Please log in.</a>'; else echo 'Logged in. Welcome back!'; ?></td>
(...)

 

It seems to work, but if I log in and click the link back home, it still says "Please log in.", meaning the condition is still True. Any ideas?

 

Your code looks a tad awry to me

 

Try it like this first:

<?php
if (!tep_session_is_registered('customer_id')) {
// if not logged in echo this
echo '<a href="http://www.fly-n-high-discgolf.com/oscommerce/catalog/login.php">Please log in.</a>';
}
else {
//else you are logged in
echo 'Logged in. Welcome back!';
}
?>

 

 

inline if statements only really work consistently when you use an if statement by itself

like:

 

if (someconditionexists) do whatever here; //all on one line no break in the line

 

also you may want to add some code in to check if the customer id is coming in etc.

 

I don't know if this will work, but it's the only thing I see as being a potential problem - other than that, and something is NOT killing the session properly: have you modified the log_out process at all?

 

Sometimes there are refresh problems also in goin back and forth - see if an F5 refresh on your browser helps kill the session.

 

Also, just clear out all your cached session stuff after you log in, just to see if it really does work.

 

Keep us posted!

Nothing unreal exists

Posted
Your code looks a tad awry to me

 

Try it like this first:

 

I think I must be missing a reference or something. Reformatting the code with line breaks made no difference.

 

I was able to confirm my code is indeed executing by removing the "!" (not) before the "!tep_session_is_registered" to force it to perform the "else" instead. I suspect the IF condition is coming up empty.

 

Here is my (very simple) PHP header:

 

<?php
/*
 $Id: index.php,v 1.1 2007/04/04 11:28:24 hpdl Exp $
*/
 chdir('oscommerce/catalog');
 require('includes/functions/sessions.php');
?>

Just a simple reference to "sessions.php". I think I "require" another script to read the "customer_id". Any idea how I can view what "tep_session_is_registered('customer_id')" is returning? Thanks again for all the help.

Posted
I think I must be missing a reference or something. Reformatting the code with line breaks made no difference.

 

I was able to confirm my code is indeed executing by removing the "!" (not) before the "!tep_session_is_registered" to force it to perform the "else" instead. I suspect the IF condition is coming up empty.

 

Here is my (very simple) PHP header:

 

<?php
/*
 $Id: index.php,v 1.1 2007/04/04 11:28:24 hpdl Exp $
*/
 chdir('oscommerce/catalog');
 require('includes/functions/sessions.php');
?>

Just a simple reference to "sessions.php". I think I "require" another script to read the "customer_id". Any idea how I can view what "tep_session_is_registered('customer_id')" is returning? Thanks again for all the help.

 

that maybe the problem - try using application_top.php in your require file. That has everything related to the session - my guess is you are not receiving the DB info or apart of some other related function that is essential to you page running properly.

 

You may run into a header conflict, if you do I can show you how to get around that as well.

Nothing unreal exists

Posted
that maybe the problem - try using application_top.php in your require file.

 

I tried that, but that just gives me the following error:

 

Fatal error: Cannot redeclare tep_session_start() (previously declared in /homepages/14/d172823197/htdocs/oscommerce/catalog/includes/functions/sessions.php:67) in /homepages/14/d172823197/htdocs/oscommerce/catalog/includes/functions/sessions.php on line 66

 

Somehow, it thinks it's already been referenced (the OSC code jumps around so much, who knows?)

 

This is killing me.

Posted
When you are logged in OS commerce sets a hashed session for you when you are logged in - you'll need to use that

 

Kirikintha,

 

The other solution provided here seems to have hit a brick wall. "!tep_session_is_registered" is not returning any value (as far as I can tell).

 

How might I use "encrypted sessions" (you previously corrected "hashed" as encrypted) to detect login status? Thanks.

Posted
Kirikintha,

 

The other solution provided here seems to have hit a brick wall. "!tep_session_is_registered" is not returning any value (as far as I can tell).

 

How might I use "encrypted sessions" (you previously corrected "hashed" as encrypted) to detect login status? Thanks.

Why you did not include the application_top.php exactly the same way as every other osc file. The only thing you need is the chdir before the application_top.php Do not include individual files unless you know what you're doing.

 

The other thing you should do is to switch off forcing cookies from your osc admin so I can see if your sessions are working. Because to me seems they don't and that will point to incorrect settings in the configure.php files.

Posted
Why you did not include the application_top.php exactly the same way as every other osc file. The only thing you need is the chdir before the application_top.php Do not include individual files unless you know what you're doing.

 

The other thing you should do is to switch off forcing cookies from your osc admin so I can see if your sessions are working. Because to me seems they don't and that will point to incorrect settings in the configure.php files.

 

Thanks again for the reply. I must be totally lost.

 

In my header:

  chdir('oscommerce/catalog/includes');
 require('functions/sessions.php');
 require('application_top.php');

 

I get an error this way. Deleting the second "require" ("application_top") is the only way around it, which is why I didn't list it before.

 

When I change the dir for "sessions.php"... "chdir('oscommerce/catalog/includes');"... I didn't include the "functions" folder in the path, instead using "require('functions/sessions.php');" (there is a "sessions.php" inside the "classes" dir too) so that I can also call other scripts outside of that folder.

 

I then "require('application_top.php');", which is in the path my "chdir" sets. I'm assuming that my chdir above is already setting the proper directory and does not need to be set again. Or do I need to change it before each "require"?

 

This is all pretty new to me, and I'm discovering just how much I *don't* know as I figure this out.

 

I switched off cookies so you can view the sessions. Thanks again for your help.

Posted

just one declaration - application_top.php only.

Do not add session.php, it is included in the application_top.php file. The error you are getting is correct, just use the application_top.php

 

Oh and for clarification, when i was referring to hashed sessions, I was referring to how it works, not necessarily another solution.

 

You are on the right track, delete the require(session.... and you should be ok!

 

chdir('oscommerce/catalog/includes');
require('application_top.php');

 

Out of curiosity, why the chdir?

Nothing unreal exists

Posted

no, that's incorrect. You do

 

chdir('oscommerce/catalog');

require('includes/application_top.php');

 

why you would change to includes? You want to change where the oscommerce files are not in the includes. Have you read post #11?

Posted
no, that's incorrect. You do

 

chdir('oscommerce/catalog');

require('includes/application_top.php');

 

why you would change to includes? You want to change where the oscommerce files are not in the includes. Have you read post #11?

 

Thanks, though this gives me the same result as before... no error, but no detection either.

 

I truncated my chdir at "includes" so I could call *both* sessions.php and application_top.php (just added "functions/" before "sessions.php". But now that I've deleted the call to "sessions", I changed my code using your lines above.

 

Either way, "tep_session_is_registered" does not seem to be returning a value. I'm stumped. :(

Posted

no, don't change anything else. So what is the link now to the script that you modified in the root of the domain? Is it privacy.php? Post the code of that script and the link. It includes the login detection right?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...