[advanced search]
 

Go Back   NamePros.com > Discussion > Web Design & Development > Programming

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.


Closed Thread
 
LinkBack Thread Tools
Old 07-31-2008, 02:15 PM   #1 (permalink)
Senior Member
 
nasaboy007's Avatar
 
Join Date: Jul 2005
Location: NJ
Posts: 1,112
1,454.30 NP$ (Donate)

nasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud of


htaccess redirect to HTTPS on select pages

I'm a complete neophyte at using the .htaccess file, but I know that you can do redirections and stuff with it. Now, I want to switch between http and https depending on what file is requested. If they go to the index of the site (http://www.mysitememberships.com OR http://www.mysitememberships.com/index.php), I want it to stay as http. However, if they go to any of, say 4, specific pages, I want it to switch over to https (pages would be /myadd.php, /myedit.php, /myview.php, /userlogin.php, /controlpanel.php). However, after they visit that page and it switches over to https, I would like it to switch back to http if they request a page that is not part of that list (such as going back to the index).

How would I go about doing this?
nasaboy007 is offline  
Old 08-01-2008, 08:41 PM   #2 (permalink)
NamePros Regular
 
Palyriot's Avatar
 
Join Date: Jul 2004
Location: Seattle, Wa
Posts: 596
76.25 NP$ (Donate)

Palyriot is a jewel in the roughPalyriot is a jewel in the roughPalyriot is a jewel in the rough


I'd say just do it in php and not your htaccess file. Just do a conditional that includes the 's' in https:// when those certain pages are linked to.
Palyriot is offline  
Old 08-03-2008, 11:38 AM   #3 (permalink)
Senior Member
 
nasaboy007's Avatar
 
Join Date: Jul 2005
Location: NJ
Posts: 1,112
1,454.30 NP$ (Donate)

nasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud of


yeah i did it with php, thanks.

my solution, put on every page:

PHP Code:
$httpsfile[] = "1"; //placeholder, idk why it doesn't read first array val
$httpsfile[] = "page1.php";
$httpsfile[] = "page2.php";
$httpsfile[] = "page3.php";
$httpsfile[] = "page4.php";
$httpsfile[] = "page5.php";
$httpsfile[] = "page6.php";
$httpsfile[] = "page7.php";

if(
$_SERVER['HTTPS'] != "on" && array_search(basename($_SERVER['PHP_SELF']), $httpsfile) != "") {
    
reload("https://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']);
}
else if(
array_search(basename($_SERVER['PHP_SELF']), $httpsfile) == "" && $_SERVER['HTTPS'] == "on") {
    
reload("http://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']);
}

since it redirects non-https-needed pages back to http, it saves server processing.
nasaboy007 is offline  
Old 08-04-2008, 06:26 AM   #4 (permalink)
NamePros Regular
 
qbert220's Avatar
 
Join Date: Jul 2007
Location: UK
Posts: 242
214.85 NP$ (Donate)

qbert220 is a glorious beacon of lightqbert220 is a glorious beacon of lightqbert220 is a glorious beacon of lightqbert220 is a glorious beacon of lightqbert220 is a glorious beacon of light


Quote:
Originally Posted by nasaboy007
PHP Code:
$httpsfile[] = "1"; //placeholder, idk why it doesn't read first array val
It does read the first entry in the array. array_search returns the key of the matching array entry. The key of the first entry will be zero. != and == are boolean operators. Zero is false when cast to a boolean, all other numbers are true.

array_search will return FALSE if it does not find a match, so you should use === and !== to test without casting like this:

PHP Code:
if($_SERVER['HTTPS'] != "on" && array_search(basename($_SERVER['PHP_SELF']), $httpsfile) !== FALSE) {
    
reload("https://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']);
}
else if(
array_search(basename($_SERVER['PHP_SELF']), $httpsfile) === FALSE && $_SERVER['HTTPS'] == "on") {
    
reload("http://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']);
}
I'd favour using in_array since you don't care what the key is:

PHP Code:
if($_SERVER['HTTPS'] != "on" && in_array(basename($_SERVER['PHP_SELF']), $httpsfile)) {
    
reload("https://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']);
}
else if(
in_array(basename($_SERVER['PHP_SELF']), $httpsfile) && $_SERVER['HTTPS'] == "on") {
    
reload("http://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']);
}
For better performance you might want to do the "in_array" once, then check the $_SERVER['HTTPS'] setting.
qbert220 is online now  
Old 08-16-2008, 09:49 AM   #5 (permalink)
NamePros Regular
 
Rudy's Avatar
 
Join Date: Jul 2005
Location: United States
Posts: 588
613.72 NP$ (Donate)

Rudy is just really niceRudy is just really niceRudy is just really niceRudy is just really nice

Save a Life
Since the thread I started here (http://www.namepros.com/website-deve...-benefits.html) is evolving into a programming thread, and it is very closely related to this thread, I think I'll post here now with a question.

I have the exact code above that qbert has posted:

Code:
if($_SERVER['HTTPS'] != "on" && in_array(basename($_SERVER['PHP_SELF']), $httpsfile)) {
    reload("https://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']);
}
else if(in_array(basename($_SERVER['PHP_SELF']), $httpsfile) && $_SERVER['HTTPS'] == "on") {
    reload("http://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']);
}
I have this in a required file that I require on every single page of my website. The problem is, when I go to test this, I'm told that the reload() function doesn't exist (and thus, a fatal PHP error is thrown). Nasaboy, do you have the code for the function too? I guess I might have a go and see if I can write the function to work, or find a native function that does the same thing... I see where you are going with this, but I'm just not exactly sure how to get there.

Edit:
I guess I could try a PHP redirect, could I not? This would work just as well, right?

Last edited by Rudy; 08-16-2008 at 09:55 AM.
Rudy is offline  
Old 08-16-2008, 09:56 AM   #6 (permalink)
Senior Member
 
nasaboy007's Avatar
 
Join Date: Jul 2005
Location: NJ
Posts: 1,112
1,454.30 NP$ (Donate)

nasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud of


oh lol sorry, my bad. i just made a reload function, basically header location. here:

PHP Code:
function reload($destination) {
    
$reload = 'Location: ' . $destination;
    
header($reload);
}


edit: just saw your edit, yes it is just a php redirect. i just put it into a function because i used it so many times throughout my site i didn't feel like typing everything out (laziness ftw).
nasaboy007 is offline  
Old 08-16-2008, 09:59 AM   #7 (permalink)
NamePros Regular
 
Rudy's Avatar
 
Join Date: Jul 2005
Location: United States
Posts: 588
613.72 NP$ (Donate)

Rudy is just really niceRudy is just really niceRudy is just really niceRudy is just really nice

Save a Life
Ahh, that makes perfect sense. I see it now. Thanks for this!
Rudy is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Site Sponsors
Advertise your business at NamePros

All times are GMT -7. The time now is 05:02 AM.


Powered by: vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0
Template-Modifications by TMS
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85