Jump to content

Need help with this short piece of my script... It is breaking my page!


physaux

Recommended Posts

Hey guys, I am trying to edit my redirection page to do split testing. I already have a mechanism to chose a random of X urls to redirect the visitor to, however I need to address a problem. What if someone clicks my link twice? I don't want them to see that they are being sent to two different pages. So what I want to do is serve the same url per IP. I am trying to accomplish this by adding a database table that records which url was generated per IP. If the same IP comes again, it is sent to its last URL. If a new IP comes, it is given a random url and added to the database. If this new IP comes a second time, it will always be served the same url.

 

I have a cron job setup to delete X day old entries in my table. I know this is not a perfect system, but it is what I want to do. Anyways, here is the related code that I have written for it. My problem is that when I add the code, even if my program never actually gets to it, it crashes my page. Nothing happens on my page.. Anyone see any problems?

 

ini_set('display_errors',1);
error_reporting(E_ALL|E_STRICT);

//in this area, a random link is generated. It is in a string variable called $chosenlink


$sticky_ip=false;
if($sticky_ip){


    $DBhost = "host";
    $DBuser = "user";
    $DBpass = "pass";
    $DBName = "database";
    $table = "table";
    $ip = $_SERVER['REMOTE_ADDR'];
    
    
    mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database");
    @mysql_select_db("$DBName") or die("Unable to select database $DBName");
    
    $sqlquery = "SELECT ip_address FROM $table WHERE ip_address = '".$ip."' LIMIT 1;";
    $results = mysql_query($sqlquery);
    if(mysql_num_rows() == 1){
        $rowoutput = mysql_fetch_array($results)
        $chosenlink = $rowoutput['url'];
    }else{
        $sqlquery = "INSERT INTO $table VALUES('".$ip."','".$chosenlink."');";
        $results = mysql_query($sqlquery);
    }
    mysql_close();
    
}

//here they are later redirected to $chosenlink

 

**I have error display on, IDK why there are no errors being displayed for this.. :(

**when I remove this section of code, everything works fine and I am sent to a random url. When I put it back, my page is dead. Nothing gets displayed, and I don't see any entries in my database when I look at it with PHPMyAdmin

Link to comment
Share on other sites

The error doesn't appear to be in this part of the code.

 

Although I do want to point out a couple things. You define a variable as false, then the entire rest of your code is ignored because it's in an if statement that only runs it if the variable defined as false is true:

 

$sticky_ip=false;

if($sticky_ip){

 

Also, when you use single quotes inside double quotes, you don't have to concatenate variables to the string, doing so causes your script to run slower. Double quotes are designed to interpret variables inside them because PHP parses the string:

 

$sqlquery = "SELECT ip_address FROM $table WHERE ip_address = '".$ip."' LIMIT 1;";

Should be:

 

$sqlquery = "SELECT ip_address FROM $table WHERE ip_address = '$ip' LIMIT 1;";

...and...

 

$sqlquery = "INSERT INTO $table VALUES('".$ip."','".$chosenlink."');";

Should be:

 

$sqlquery = "INSERT INTO $table VALUES('$ip','$chosenlink');";

Link to comment
Share on other sites

thanks for explaining about the quotes, I am going to make those changes.

 

The reason I have the code inside of the if statement

 

$sticky_ip=false;

if($sticky_ip){

 

Is because I want to be able to toggle it. I don't want to activate it yet. However, when I was testing it, I had it on true. I am going to make these changes and see if it does anything. I'll report back.

Link to comment
Share on other sites

My suggestions won't actually fix any errors as there was nothing expressly wrong with your initial script, it just didn't utilize double quotes efficiently.

 

In order to help you diagnose the problem, we'll need to see more of the PHP in the file.

Link to comment
Share on other sites

Well, I would expect there to be "some" error. In this case I would expect a PHP and/or MySQL error followed by an error about not being able to send headers after output has been sent to the browser.

 

Here is one place that should definitely produce an error:

if(mysql_num_rows() == 1){

That command has a required parameter of the resource link. It should be:

 

if(mysql_num_rows($results) == 1){

 

I see some other problems as well. In the ELSE condition you are trying to do an insert of $chosenlink, but you define that in the IF condition.

 

Also, this line is missing a semi-colon:

$rowoutput = mysql_fetch_array($results)

 

Also, why are you deleting the records. If you have a timestamp, then formulate the queries in your application to ignore records older than x days.

Link to comment
Share on other sites

Depending on the rest of the code, and the exact nature of the problem, it's possible for PHP to return a white page (no HTML). I've had this happen to me once or twice.

 

Although I did totally miss the semicolon and the required param, doh! Thanks for the save mjdamato!

Link to comment
Share on other sites

I have been commenting out sections of the code and testing it (with $sticky_ip=false; )

 

I have concluded that the error is somewhere in here:

 

if(mysql_num_rows() == 1){
        $rowoutput = mysql_fetch_array($results)
        $chosenlink = $rowoutput['url'];
    }else{

 

Thanks to the above comment, I changed this line:

 

if(mysql_num_rows($results) == 1){

 

 

The $chosenlink variable is actually already defined with a random link. This script is only re-defining it if the IP has visited before.

 

Also I added the semicolon to this line:

 

$rowoutput = mysql_fetch_array($results)

 

 

I saved and tested, and it did not crash! Thanks for helping me guys in fixing this. It works as I wanted it to now :)

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.