Welcome Guest, Not a member yet? Register   Sign In
form_open() and www "subdomain"
#1

[eluser]jant90[/eluser]
I'm very new to CI and still following tutorials to get familiar with it but I already think it's awesome and I'm very eager to develop my first site with it!

While following a tutorial I got introduced to "form_open()". Let's say you run form_open('submit') on the homepage of example.com, with your base_url set to http://example.com. That will generate the following form action:
Quote:http://example.com/submit

But what if the user is on www.example.com, then the generated form action will be the same.

Now let's put this into a real life setting, let's say a user is logged in and want's to post a message in response to a blog post. When he is logged in on www.example.com and the login is stored in a session the user will be redirected to example.com/submit (without www), there the session does not exist and thus the user is not logged in. The result: the message won't get posted.

Another way would be to use _SERVER["HTTP_HOST"] and generate a form like this:
Quote:form_open('http://'.$_SERVER["HTTP_HOST"].'/submit')

But it doesn't look that clean so I was thinking a solution could be to remove www altogether (or permanently add it) by using a url rewrite in htaccess. As I don't like the www prefix anyways I want it like that and I was wondering if anyone can help me with that as I always seem to manage to mess up .htacces files. I already took the .htaccess file from the user guide to remove index.php and I want to extend that one, how do I remove the www prefix on any url the user types in?
#2

[eluser]tonanbarbarian[/eluser]
if setup correctly your config.php file should contain the base url for the site
so if you do
Code:
form_open('submit');
it should automatically create the following html
Code:
<form method="post" action="http://example.com/index.php/submit" />

the action may be http://example.com/submit if you have the index.php removed from your urls

if you need this to work for multiple subdomains then you need to have some code in your config.php to crreate the base_url that you want for the site.

Personally I am not sure what the problem is exactly because I HATE www subdomains. I prefer all my site to be example.com not www.example.com. So if someone comes to my sites as www.example.com either the server redirects them, or after they submit the form they will be redirected to example.com
#3

[eluser]jant90[/eluser]
Yes, I want to redirect them from www.example.com to example.com too, because I hate the www subdomain too.

But the problem is that a session on www.example.com doesn't exist on example.com, so when users get redirected after hitting submit they are not logged in anymore (or whatever you have stored in a session is lost) meaning that your site doesn't work as expected.

So I'm curious how you'd fix this (using .htaccess).
#4

[eluser]dbashyal[/eluser]
check this thread: http://ellislab.com/forums/viewthread/85429/
#5

[eluser]jant90[/eluser]
That works awesome, thanks!
#6

[eluser]pbreit[/eluser]
Would it work to switch base_url to '/'?




Theme © iAndrew 2016 - Forum software by © MyBB