Ahsan Roy
Ahsan Roy

Reputation: 201

Setting base_url() in php . What should be the base_url() in any framework of php?

My question not focus on method of how base url is set in any framework of php. Rather what I actually want to know is what should be the base_url() From two options below

Suppose I select option 1 i.e http://example.com all the traffic to my website coming from http://www.example.com will face following problems, similarly if i select option 2 traffic coming from other otpion's url will face problem below.

Problem

Ajax blocked by browsers due to CORS(Cross Origin Request Sharing). Because google treats www and non www requests as from two different websites. My ajax code is below if that can be of any help

$.ajax({
url: '<?php echo base_url(); ?>/Index/perform_task',
type:'POST',
data: {
    name: $("#name_field").val(),
    task: $("#task_field").val(),

},
success:function(data){
    if (data == 'task_completed'){
            window.location.href = "<?php echo base_url() ?>";
        }
        else{
            $('#task_error').html('Some Error occured.')        
        }
}

});

Upvotes: 1

Views: 558

Answers (1)

Bastien Ho
Bastien Ho

Reputation: 829

As @junkfoodjunkie said, you should only use ONE domain and redirect the other. But if, for any reason, you have to keep the two domains, then you should call your ajax script within the current domain.

Then, your base_url() function should return the current domain. for example:

function base_url(){
    return 'http'.(isset($_SERVER['HTTPS'])?'s':'').'://'.$_SERVER['SERVER_NAME'];
}

Upvotes: 1

Related Questions