1
if(strpos($_SERVER['HTTP_HOST'],"localhost")>=0)
  {
   $con = mysql_connect('localhost', 'root', '');
    if (!$con)
     {
       die('Could not connect: ' . mysql_error());
      }
  } 
else if(strpos($_SERVER['HTTP_HOST'],"www.mydomain.com")>=0)
    {
       $con = mysql_connect('localhost', 'Eexam', 'exam');
       if (!$con)
       {
        die('Could not connect: ' . mysql_error());
       }
    }

This code is making a connection in localhost but doesn't working in my live server . I will appreciate you if have a solution.

Muhammad Ashikuzzaman
  • 3,075
  • 6
  • 29
  • 53
  • 2
    Please be aware that the mysql extension (supplying the mysql_ functions) has been deprecated since 2012, in favor of the mysqli and PDO extensions. It's use is highly discouraged. See http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php – Oldskool May 29 '14 at 13:44
  • 4
    have you checked if the `$_SERVER['HTTP_HOST']` applies with any of your conditions? – Klemen Tusar May 29 '14 at 13:45
  • `mysql_connect('localhost', 'Eexam', 'exam');` Is `localhost` a valid connection on your webserver? – War10ck May 29 '14 at 13:46
  • Yes if I print then it is printing the domain name in live server. – Muhammad Ashikuzzaman May 29 '14 at 13:46
  • Might the strpos function return 0 by any chance? – Chris Lear May 29 '14 at 13:46
  • No if I only connect with "$con = mysql_connect('localhost', 'baseer_Eexam', 'baseer');" .It's working.@ War10ck – Muhammad Ashikuzzaman May 29 '14 at 13:49
  • What's wrong with using `if($_SERVER['SERVER_NAME'] == "www.mydomain.com"){` instead of all this strpos malarky? – The Blue Dog May 29 '14 at 13:57
  • I don't under stand who vote it down? I think this is not a rubbish question. But who did that . Please do not close the way for beginner.Make mind soft for novice.If it will happen I will not be able to question again. – Muhammad Ashikuzzaman May 29 '14 at 13:57
  • Another man vote it down but why I don't understand. I did not got my answer yet. – Muhammad Ashikuzzaman May 29 '14 at 14:02
  • 1
    @Md.Ashikuzzaman Everything boils down to if(strpos($_SERVER['HTTP_HOST'],"www.mydomain.com")>=0) but we have no idea what $_SERVER['HTTP_HOST'] is so how exactly can we help you? When someone asked what $_SERVER['HTTP_HOST'] is currently you responded "Domain name , Please read the question attentively" which is quite rude to someone trying solve your problem as its really the only way to know what's wrong – Halfwarr May 29 '14 at 14:03
  • Please $_SERVER['HTTP_HOST'],"www.mydomain.com" I did not mean like what you think . I just tell it frankly and know they are like a boss of mine . So I have came here. But you redirect it in different mind , sorry . – Muhammad Ashikuzzaman May 29 '14 at 14:07
  • Thanks every one for helping me . I have got solution. The problem was using output before header in that mysql connection script page . Thanks to stack overflow. – Muhammad Ashikuzzaman May 29 '14 at 14:14
  • Thanks Halfwar for instructing me @Halfwarr – Muhammad Ashikuzzaman May 29 '14 at 14:35

2 Answers2

0

I would guess because the condition below is not met:

if(strpos($_SERVER['HTTP_HOST'],"www.mydomain.com")>=0)

Try to check the content of $_SERVER['HTTP_HOST'] with:

echo $_SERVER['HTTP_HOST'];

Or you can switch to an if/else like:

if (strpos($_SERVER['HTTP_HOST'], 'localhost') >== 0)
{
    //Development connexion here...
} else {
    //Production connexion here...
}
AlexV
  • 22,658
  • 18
  • 85
  • 122
  • Yes it is being print. Also header in condition is not working in which page I include this code. – Muhammad Ashikuzzaman May 29 '14 at 13:50
  • Can you report what is exactly in $_SERVER['HTTP_HOST']? Does a "Could not connect" is shown? – AlexV May 29 '14 at 13:53
  • 1
    Yeah but please understand that you are not helping by not answering questions... If the www is not in the HTTP_HOST your condition is not met and you have your problem – AlexV May 29 '14 at 13:55
  • No , when I print then the output is www.mydomain.com . But when i am adding the code for database connection or header in the condition that doesn't working. – Muhammad Ashikuzzaman May 29 '14 at 14:01
  • So you see "Could not connect" message then? – AlexV May 29 '14 at 14:02
  • even this condition is also not working. if(isset($_POST['keepme_login'])) { echo "something"; setcookie("email",$email,time()+36000); setcookie("password",$password,time()+36000); header('Location:index.php'); } else { $verify=1; header('Location:verify.php'); } – Muhammad Ashikuzzaman May 29 '14 at 14:04
  • This code has nothing to do with your question and just confuse me (and that code won't work - can't change headers when content has been sent - unless you buffer your output which I doubt). Try to stay on topic if you want help :) – AlexV May 29 '14 at 14:09
  • Ok, sorry. I have got a solution in which page I used including the database connection page in same page I used header .But before header I used echo means an output function it is the faulty point. Is n't it , am also confused. I find this some where in stackoverflow. Thanks for helping me . – Muhammad Ashikuzzaman May 29 '14 at 14:12
  • Also your testing procedure help me . thanks lot . i am leaving but accepting your answer.@AlexV – Muhammad Ashikuzzaman May 29 '14 at 14:27
0

You can try:

if(strpos($_SERVER['HTTP_HOST'],"mydomain.com") != FALSE)
Adrian B
  • 1,490
  • 1
  • 19
  • 31