Jump to content



Photo
* * * * * 3 votes

Useful Snippets of Code


  • Please log in to reply
49 replies to this topic

#21   cannuck1964

cannuck1964

    Contract Coder

  • Partner
  • 1,145 posts
  • Real Name:Peter McGrath
  • Gender:Male
  • Location:Ontario, Canada

Posted 03 January 2013 - 18:25

for .htaccess



# Eliminates duplicate home page i.e.: www.domain.com/index.php and www.domain.com appear as duplicate home page

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://www.domain.com/ [R=301,L]

Peter McGrath
-----------------------------

See my Profile (click here) for more information and to contact me for professional osCommerce support that includes SEO development, custom development and security implementation

#22   cannuck1964

cannuck1964

    Contract Coder

  • Partner
  • 1,145 posts
  • Real Name:Peter McGrath
  • Gender:Male
  • Location:Ontario, Canada

Posted 03 January 2013 - 18:29

for .htaccess

# Disable http track for PCI compliance
RewriteCond %{REQUEST_METHOD} ^TRACK
RewriteRule .* - [F]

Peter McGrath
-----------------------------

See my Profile (click here) for more information and to contact me for professional osCommerce support that includes SEO development, custom development and security implementation

#23   mattjt83

mattjt83
  • Members
  • 519 posts
  • Real Name:Matt Toste
  • Gender:Male
  • Location:CA, USA

Posted 03 January 2013 - 22:15

@cannuck1964

In addition to your last post this works pretty well for the same purpose in your .htaccess:

#only allow GET, POST, HEAD requests
<LimitExcept GET POST HEAD>
Deny from all
</LimitExcept>

Matt


#24   cannuck1964

cannuck1964

    Contract Coder

  • Partner
  • 1,145 posts
  • Real Name:Peter McGrath
  • Gender:Male
  • Location:Ontario, Canada

Posted 04 January 2013 - 14:48

for htaccess


Add this in to stop scripts from running in folders that should not have scripts in them ie images, downloads, tmp, etc

This adds a layer of security to the folder (s) to stop some hacking techniques employed.


# stop scripts from running from the folder
IndexIgnore *
Options All -Indexes
# Secure directory by disabling script execution
AddHandler cgi-script .php .php2 .php3 .php4 .php5 .php6 .php7 .php8 .pl .py .jsp .asp .htm .html .shtml .sh .cgi
Options -ExecCGI
# Don't show this file, that would be bad as well!
<Files .htaccess>
order allow,deny
deny from all
</Files>

Peter McGrath
-----------------------------

See my Profile (click here) for more information and to contact me for professional osCommerce support that includes SEO development, custom development and security implementation

#25   Web-Source 5

Web-Source 5

    &curren;

  • Members
  • 18 posts
  • Real Name:G.L.Walker
  • Gender:Male
  • Location:Texas

Posted 06 January 2013 - 17:35

alternate .htaccess rules, simular to Peters, but with no need to input the actual FQD.
# Option 1:
# Rewrite "www.example.com -> example.com"
<IfModule mod_rewrite.c>
  RewriteCond %{HTTPS} !=on
  RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
  RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
</IfModule>
# Option 2:
# To rewrite "example.com -> www.example.com" uncomment the following lines.
# Be aware that the following rule might not be a good idea if you
# use "real" subdomains for certain parts of your website.
#<IfModule mod_rewrite.c>
#  RewriteCond %{HTTPS} !=on
#  RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
#  RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
#</IfModule>


#26   Web-Source 5

Web-Source 5

    &curren;

  • Members
  • 18 posts
  • Real Name:G.L.Walker
  • Gender:Male
  • Location:Texas

Posted 06 January 2013 - 17:36

More PCI compliance for .htaccess:

<IfModule mod_rewrite.c>
  Options +FollowSymlinks
  RewriteEngine On
#RewriteCond %{SERVER_PORT} !^443$
#RewriteRule ^account.php https://example-domain-please-change-me.com/account.php [R=301,L]
#
#RewriteCond %{SERVER_PORT} !^443$
#RewriteRule ^affiliate_affiliate.php https://example-domain-please-change-me.com/affiliate_affiliate.php [R=301,L]
#
#RewriteCond %{SERVER_PORT} !^443$
#RewriteRule ^affiliate_summary.php https://example-domain-please-change-me.com/affiliate_summary.php [R=301,L]
#
#RewriteCond %{SERVER_PORT} !^443$
#RewriteRule ^login.php https://example-domain-please-change-me.com/login.php [R=301,L]
#
#RewriteCond %{SERVER_PORT} !^443$
#RewriteRule ^create_account.php https://example-domain-please-change-me.com/create_account.php [R=301,L]
#
#RewriteCond %{SERVER_PORT} !^443$
#RewriteRule ^checkout_confirmation.php https://example-domain-please-change-me.com/checkout_confirmation.php [R=301,L]
</IfModule>


#27   Web-Source 5

Web-Source 5

    &curren;

  • Members
  • 18 posts
  • Real Name:G.L.Walker
  • Gender:Male
  • Location:Texas

Posted 07 January 2013 - 17:22

Heres a handy snippet for adding product to cart from an external URL, or just in plain HTML format:
<a href="http:/your-site.com/index.php/cPath/#/action/buy_now/products_id/#">Add To Cart</a>
Replace "#" with the category id and product id.

#28   burt

burt

    I drink and I know things

  • Community Team
  • 12,504 posts
  • Real Name:G Burton
  • Gender:Male
  • Location:UK/DEV/on

Posted 30 January 2013 - 18:55

PHPMYADMIN

Update weight of all products in a known category;

update products set products_weight = 'Y' where products_id in (select products_id from products_to_categories where categories_id = X)

Change Y to the desired weight and X to the ID of the category.

This is a signature that appears on all my posts.  It is not specifically aimed at you.

 

IF YOU MAKE A POST REQUESTING HELP...please state the exact version of osCommerce that you are using. THANKS
 
If you are still on the old style osCommerce, it is time to move to Responsive.

 


#29   burt

burt

    I drink and I know things

  • Community Team
  • 12,504 posts
  • Real Name:G Burton
  • Gender:Male
  • Location:UK/DEV/on

Posted 13 February 2013 - 12:25

I needed to ensure that stock does not reduce below zero, as part of my barcode system for shops that are both online and high street. This takes care of it;

tep_db_query("update " . TABLE_PRODUCTS . " set products_quantity = GREATEST(products_quantity-1, 0) where products_upc = '" . $scanned_upc . "'");

Now if the stock in the high street shop is sitting at 1 piece and it is scanned (making the stock go to zero), any mistaken scan would mean the stock stays at zero. Eg, if the assistant makes a mistake and scans the barcode twice. We need to ensure that stock does not go into negative numbers so that when stock is replenished (via barcode), the stock level is correct.

Example: stock sits at 1. Accidentally scanned twice. Stock now sits at -1 (assume there is no failsafe to keep stock at zero). High Street gets new stock comprising 3 items. These are scanned in to update stock. Stock now sits at 2. But there are actually 3 for sale;

-1 + 3 = 2

whereas, with the failsafe;
0 + 3 = 3

This is a signature that appears on all my posts.  It is not specifically aimed at you.

 

IF YOU MAKE A POST REQUESTING HELP...please state the exact version of osCommerce that you are using. THANKS
 
If you are still on the old style osCommerce, it is time to move to Responsive.

 


#30   bruyndoncx

bruyndoncx

    osCommerce Teenager

  • Members
  • 3,786 posts
  • Real Name:Carine Bruyndoncx
  • Gender:Female
  • Location:Belgium/ Antwerp/ Turnhout/ Arendonk

Posted 13 February 2013 - 12:53

PHPMYADMIN

Update weight of all products in a known category;

update products set products_weight = 'Y' where products_id in (select products_id from products_to_categories where categories_id = X)

Change Y to the desired weight and X to the ID of the category.

Not exactly a tip, but one of my customizations is the addition of a categories_weight field ( a field cloned from sort_order) and when a products_weight is missing, it defaults to the categories weight, if that is missing, it tels the customer to contact us as the item is missing data to provide an accurate shipping quote.

KEEP CALM AND CARRY ON
FYI Upgrade to the highest PHP version you can( PHP 5.5/5.6 or 7.1  and get big performance improvements for free)

But be aware php 5.5 is more strict about things.
UTF8-without BOM, no extra spaces allowed at the beginning or end of your php file, or your redirects wont work.
No double declarations of functions allowed - used to slip through the cracks ...

 

Find the most frequent unique errors to fix:

grep "PHP" php_error_log.txt | sed "s/^.* PHP/PHP/g" |grep "line" |sort | uniq -c | sort -r > counterrors.txt


#31   Juto

Juto
  • Members
  • 369 posts
  • Real Name:Sara
  • Gender:Female

Posted 13 February 2013 - 14:48

Replace target="_blank" with the correct javascript syntax (example):

<a href="http://www.yourdomai...m/yourfile.php" onclick="window.open(this.href); return false;">your link text</a>

Sara

#32   Juto

Juto
  • Members
  • 369 posts
  • Real Name:Sara
  • Gender:Female

Posted 13 February 2013 - 14:53

w3c compliance
Replace all instances of 'hspace="5" vspace="5"' with ' class="image-margins"'
and add the class to your stylesheet.

Sara

#33   burt

burt

    I drink and I know things

  • Community Team
  • 12,504 posts
  • Real Name:G Burton
  • Gender:Male
  • Location:UK/DEV/on

Posted 19 February 2013 - 10:24

All of the 2 country ISO codes, which I needed to find but could not find (admittedly did not try very hard):

ALL ISO's

AF,AX,AL,DZ,AS,AD,AO,AI,A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,CD,CK,CR,CI,CU,CW,
DJ,DM,DO,
EC,EG,SV,GQ,ER,ET,
FK,FO,FJ,
GF,PF,TF,GA,GM,GE,GH,GI,GL,GD,GP,GU,GT,GG,GN,GW,GY,
HT,HM,VA,HN,HK,
IS,IN,ID,IR,IQ,IM,IL,
JM,JP,JE,JO,
KZ,KE,KI,KP,KR,KW,KG,
LA,LB,LS,LR,LY,LI,
MO,MK,MG,MW,MY,MV,ML,MH,MQ,MR,MU,YT,MX,FM,MD,MC,MN,ME,MS,MA,MZ,MM,
NA,NR,NP,NC,NZ,NI,NE,NG,NU,NF,MP,NO,
OM,
PK,PW,PS,PA,PG,PY,PE,PH,PN,PR,
QA,
RE,RU,RW,BL,SH,KN,LC,MF,PM,VC,WS,
SM,ST,SA,SN,RS,SC,SL,SG,SX,SB,SO,ZA,GS,SS,LK,SD,SR,SJ,SZ,CH,SY,
TW,TJ,TZ,TH,TL,TG,TK,TO,TT,TN,TR,TM,TC,TV,
UG,UA,AE,UM,US,UY,UZ,
VU,VE,VN,VG,VI,
WF,EH,
YE,
ZM,ZW

Edited by burt, 27 May 2015 - 10:34.

This is a signature that appears on all my posts.  It is not specifically aimed at you.

 

IF YOU MAKE A POST REQUESTING HELP...please state the exact version of osCommerce that you are using. THANKS
 
If you are still on the old style osCommerce, it is time to move to Responsive.

 


#34   cannuck1964

cannuck1964

    Contract Coder

  • Partner
  • 1,145 posts
  • Real Name:Peter McGrath
  • Gender:Male
  • Location:Ontario, Canada

Posted 19 February 2013 - 15:05

@burt

Not quite sure how that is a useful code snippet...

But on another note, you can use GEOIP to ban countries countries IPs from going to your site. Just add this to your htaccess file:


<IfModule mod_geoip.c>
  GeoIPEnable ON
  # add one line for each country you wish to block
  #Vietanm
  SetEnvIF GEOIP_COUNTRY_CODE VN BlockThese
  #russia
  SetEnvIF GEOIP_COUNTRY_CODE RU BlockThese
  #romania
  SetEnvIF GEOIP_COUNTRY_CODE RO BlockThese
  #turkey
  SetEnvIF GEOIP_COUNTRY_CODE TR BlockThese
  #China
  SetEnvIF GEOIP_COUNTRY_CODE CN BlockThese
  #Nigeria
  SetEnvIF GEOIP_COUNTRY_CODE NG BlockThese
  #Iran
  SetEnvIF GEOIP_COUNTRY_CODE IR BlockThese
  Deny from env=BlockThese
</IfModule>



Peter McGrath
-----------------------------

See my Profile (click here) for more information and to contact me for professional osCommerce support that includes SEO development, custom development and security implementation

#35   burt

burt

    I drink and I know things

  • Community Team
  • 12,504 posts
  • Real Name:G Burton
  • Gender:Male
  • Location:UK/DEV/on

Posted 19 February 2013 - 17:05

@cannuck1964 - will be very useful for me if for no-one else ;)

This is a signature that appears on all my posts.  It is not specifically aimed at you.

 

IF YOU MAKE A POST REQUESTING HELP...please state the exact version of osCommerce that you are using. THANKS
 
If you are still on the old style osCommerce, it is time to move to Responsive.

 


#36   burt

burt

    I drink and I know things

  • Community Team
  • 12,504 posts
  • Real Name:G Burton
  • Gender:Male
  • Location:UK/DEV/on

Posted 21 February 2013 - 11:36

update products set products_price = (products_price*Y) where products_id in (select products_id from products_to_categories where categories_id = X)

increase or decrease prices by a percentage, of all products in a given category.

Change y to the increase/decrease (eg: make prices 20% more = 1.2), change x to the id of the category to change.

This is a signature that appears on all my posts.  It is not specifically aimed at you.

 

IF YOU MAKE A POST REQUESTING HELP...please state the exact version of osCommerce that you are using. THANKS
 
If you are still on the old style osCommerce, it is time to move to Responsive.

 


#37   burt

burt

    I drink and I know things

  • Community Team
  • 12,504 posts
  • Real Name:G Burton
  • Gender:Male
  • Location:UK/DEV/on

Posted 11 March 2013 - 13:53

Update price of products by a given manufacturer.

update `products` set `products_price` = (`products_price` * 'x.yz') WHERE `manufacturers_id` = 'w';

Change x.yz to the increase amount, eg increase prices by 15% would be 1.15.
Change w to the ID of the manufacturer.

Edited by burt, 11 March 2013 - 13:54.

This is a signature that appears on all my posts.  It is not specifically aimed at you.

 

IF YOU MAKE A POST REQUESTING HELP...please state the exact version of osCommerce that you are using. THANKS
 
If you are still on the old style osCommerce, it is time to move to Responsive.

 


#38   Gergely

Gergely

    Json Juggler

  • Community Team
  • 2,125 posts
  • Real Name:Gergely Tóth
  • Gender:Male
  • Location:Budapest

Posted 11 March 2013 - 16:11

Before MySql update training in Workbrench

SET SQL_SAFE_UPDATES=0;

Next PHP changes will kill the current codes on the following years. We should do programing for the future and never stick in the present.

My addons: Conversion Tools::Hungarian Translation::Email Templates::URL redirection
 
Development Works: Setup Languages::Email Templates::Languages from ini files::Parcel Shops::Facebook App
 
What core codes have been complained?

In orders table payment_methods value would be better if payment class name used than payment's language name.
In the orders class we found order status does not contains $order->info['orders_status'] but instead there is $order->info['orders_status_name'], and that property is language dependant.
We can not identify in order the customer language.


#39   motorcity

motorcity
  • Members
  • 241 posts
  • Real Name:Joe
  • Location:Detroit, Michigan USA

Posted 22 March 2013 - 11:03

Somewhere I heard it might hurt you to ignore ip canonicalization, This addon to .htaccess is supposed to fix that. Any comments from you SEO folks?
# Eliminates www vs. non-www
RewriteCond %{HTTP_HOST} ^domain\.com [OR]
RewriteCond %{HTTP_HOST} ^123\.45\.67\.89
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=permanent,L]


edit; I guess you really need the [OR] to get this working!

Edited by motorcity, 22 March 2013 - 11:12.


#40   motorcity

motorcity
  • Members
  • 241 posts
  • Real Name:Joe
  • Location:Detroit, Michigan USA

Posted 23 March 2013 - 02:50

Here's a different one. Directly backing up a table from a table in phpmyadmin / sql
UPDATE products p, 0214products p2
SET p.products_price = p2.products_price,
p.products_weight = p2.products_weight,
p.products_length = p2.products_length,
p.products_width = p2.products_width,
p.products_height = p2.products_height,
p.suggested_retail = p.suggested_retail
WHERE p.products_id = p2.products_id
AND p.products_ready_to_ship = 1
AND p.products_id = 1
The key here is understanding I'm using "p.products_ready_to_ship = 1" as a switch. I sorted the database and those products with corrupted data became "p.products_ready_to_ship = 1" first.
The second thing is the last part of the where; "AND p.products_id = 1" this is just a test row I'm watching as I run the query. If it doesn't work right I only have one row (I hopefully jotted down the undisturbed details prior to running the query - should go without saying right?).
No doubt this is dangerous. Create test tables by copying real ones. Test those first.