Jump to content

Recommended Posts

Hi, I have this script but am having a problem with the else statement. I think this is because several other if and else statements are used in the script.

 

When I run it I get both the page that is meant to be displayed:

 

Restore Database

Select the database you wish to restore from the menu below

 

FORM MENU

 

and the error message:

 

You are not authorised to access this page

 

instead of one or the other.

 

Here is the script:

 

<?php
if(isset($_COOKIE['sbadmin'])) {

admin_header();


# INCLUDE THE FILE CONTAINING THE MYSQL CONNECTION VARIABLES

include_once("data/required.php");

# CONNECT TO MYSQL DATABASE

if (!$error)
{ $link = @mysql_connect($dbhost,$dbuser,$dbpass);
if ($link) 
$db = mysql_select_db($db);
if (!$link || !$db) 
{ 
echo ("<p class=\"error\">The following error occured: ".mysql_error()."</p>\n");
$error=true;
}
} else { 
$link = false;
}

// REQUIRED SETTINGS

$linesThisSession  = 3000;   
                        
define ('DATA_CHUNK_LENGTH',16384); 

foreach ($_REQUEST as $key => $val) 
{
  $val = preg_replace("/[^_A-Za-z0-9-\.&= ]/i",'', $val);
  $_REQUEST[$key] = $val;
}

$error = false;
$file  = false;

# CREATES THE DATABASE SELECTION FORM

if ($filename=="")
{ 

$backupFileDir = "backup/";
$handle = opendir($backupFileDir);

while ($filename = readdir($handle))
{
$filenames[] = $filename;
}

closedir($handle);
sort($filenames);
print "</br>\n";
print "<h2 class=style2 align=center>Restore Database</h2>\n";
print "<p class=style2>Select the database you wish to restore from the menu below</p>";
print "<form action=\"".$_SERVER["PHP_SELF"]."?cmd=sqlimport&import=1&filename=$filename&\" method='POST'>";
print "<table bgcolor=#0066FF align=center>";
print "<tr><td>";
print "<select name='filename'>";
print "<option>Choose Database Backup</option>";
foreach ($filenames as $filename) {
if (($filename != ".") And ($filename != "..")) {
print sprintf("<option value='$backupFile%s'>%s</option>", $filename, $filename);
}
}
print "</select></td>";
print "<td><input type='submit' name='import' value='Start Import'></td></tr>";
print "</table></form>";
print "</center>";

print '
</td>
        </tr>
      </table>';
           
      }	
      
# DEFINES THE FILENAME POSTED FROM THE DATABASE SELECTION FORM

$filename = 'backup/'.$_POST['filename']; 

# CHECK THAT SELECTED FILE IS A .GZ FILE

  if (eregi("\.gz$",$filename)) 
    $gzFile=true;
  else
    $gzFile=false;

  ((!$gzFile && !$file=@fopen($filename,"rt")) || ($gzFile && !$file=@gzopen($filename,"rt")));

# START IMPORTING BACKUP TO DATABASE

if (!$error && isset($_REQUEST["import"]) && eregi("(\.(gz))$",$filename))

$lineNum=$_REQUEST["import"];
$querylines=0;

# PROCESS IF MAX LINES ARE NOT REACHED

while ($lineNum<$_REQUEST["import"]+$linesThisSession || $query!="")
{

$dumpline = "";
while (!feof($file) && substr ($dumpline, -1) != "\n")
{ if (!$gzFile)
$dumpline .= fgets($file, DATA_CHUNK_LENGTH);
else
$dumpline .= gzgets($file, DATA_CHUNK_LENGTH);
}
if ($dumpline==="") break;

# REMOVE \\ FROM DUMPLINE
      
$noslash = str_replace ("\\\\","",$dumpline);

$query .= $dumpline;

# EXECUTE THE QUERY IF END OF QUERY IS REACHED    

if (ereg(";$",trim($dumpline)))
{ if (!mysql_query(trim($query), $link))
{ echo ("<p class=\"error\">Error at the line $lineNum: ". trim($dumpline)."</p>\n");
echo ("<p>Query: ".trim(nl2br(htmlentities($query)))."</p>\n");
echo ("<p>MySQL: ".mysql_error()."</p>\n");
$error=true;
break;
}
$query="";
}
$lineNum++;
}
  
# CONFIRM DATABASE HAS BEEN RESTORED AND THEN RESTART THE SCRIPT

if ($lineNum<$_REQUEST["import"]+$linesThisSession)
{ 
echo ("<p class=\"success\">MySQL Database: $filename has been restored successfully.</p>\n");
}

if ($link) mysql_close();
if ($file && !$gzFile) fclose($file);
else if ($file && $gzFile) gzclose($file);

} else {

print "<p class=style2>You are not authorised to access this page</p>";
}

break;
?>

 

How can I fix this?

Thanks

Link to comment
https://forums.phpfreaks.com/topic/151637-else-error/
Share on other sites

Are you sure your not including it twice?

 

Also i'm not sure what your attempting here

((! $gzFile && ! $file = @fopen($filename, "rt")) || ($gzFile && ! $file = @gzopen($filename, "rt")));

its a conidtion to nothing!

 

in anycase i cleaned up the code formatting

<?php
if (isset($_COOKIE['sbadmin'])) {
    admin_header();
    # INCLUDE THE FILE CONTAINING THE MYSQL CONNECTION VARIABLES
    include_once ("data/required.php");
    # CONNECT TO MYSQL DATABASE
    if (! $error) {
        $link = @mysql_connect($dbhost, $dbuser, $dbpass);
        if ($link)
            $db = mysql_select_db($db);
        if (! $link || ! $db) {
            echo ("<p class=\"error\">The following error occured: " . mysql_error() . "</p>\n");
            $error = true;
        }
    } else {
        $link = false;
    }
    // REQUIRED SETTINGS
    $linesThisSession = 3000;
    define('DATA_CHUNK_LENGTH', 16384);
    foreach ($_REQUEST as $key => $val) {
        $val = preg_replace("/[^_A-Za-z0-9-\.&= ]/i", '', $val);
        $_REQUEST[$key] = $val;
    }
    $error = false;
    $file = false;
    # CREATES THE DATABASE SELECTION FORM
    if ($filename == "") {
        $backupFileDir = "backup/";
        $handle = opendir($backupFileDir);
        while ($filename = readdir($handle)) {
            $filenames[] = $filename;
        }
        closedir($handle);
        sort($filenames);
        print"</br>\n";
        print"<h2 class=style2 align=center>Restore Database</h2>\n";
        print"<p class=style2>Select the database you wish to restore from the menu below</p>";
        print"<form action=\"" . $_SERVER["PHP_SELF"] . "?cmd=sqlimport&import=1&filename=$filename&\" method='POST'>";
        print"<table bgcolor=#0066FF align=center>";
        print"<tr><td>";
        print"<select name='filename'>";
        print"<option>Choose Database Backup</option>";
        foreach ($filenames as $filename) {
            if (($filename != ".") and ($filename != "..")) {
                printsprintf("<option value='$backupFile%s'>%s</option>", $filename, $filename);
            }
        }
        print"</select></td>";
        print"<td><input type='submit' name='import' value='Start Import'></td></tr>";
        print"</table></form>";
        print"</center>";
        print'
</td>
        </tr>
      </table>';
    }
    # DEFINES THE FILENAME POSTED FROM THE DATABASE SELECTION FORM
    $filename = 'backup/' . $_POST['filename'];
    # CHECK THAT SELECTED FILE IS A .GZ FILE
    if (eregi("\.gz$", $filename))
        $gzFile = true; else
        $gzFile = false;
    ((! $gzFile && ! $file = @fopen($filename, "rt")) || ($gzFile && ! $file = @gzopen($filename, "rt")));
    # START IMPORTING BACKUP TO DATABASE
    if (! $error && isset($_REQUEST["import"]) && eregi("(\.(gz))$", $filename))
        $lineNum = $_REQUEST["import"];
    $querylines = 0;
    # PROCESS IF MAX LINES ARE NOT REACHED
    while ($lineNum < $_REQUEST["import"] + $linesThisSession || $query != "") {
        $dumpline = "";
        while (! feof($file) && substr($dumpline, - 1) != "\n") {
            if (! $gzFile)
                $dumpline .= fgets($file, DATA_CHUNK_LENGTH); else
                $dumpline .= gzgets($file, DATA_CHUNK_LENGTH);
        }
        if ($dumpline === "")
            break;
            # REMOVE \\ FROM DUMPLINE
        $noslash = str_replace("\\\\", "", $dumpline);
        $query .= $dumpline;
        # EXECUTE THE QUERY IF END OF QUERY IS REACHED    
        if (ereg(";$", trim($dumpline))) {
            if (! mysql_query(trim($query), $link)) {
                echo ("<p class=\"error\">Error at the line $lineNum: " . trim($dumpline) . "</p>\n");
                echo ("<p>Query: " . trim(nl2br(htmlentities($query))) . "</p>\n");
                echo ("<p>MySQL: " . mysql_error() . "</p>\n");
                $error = true;
                break;
            }
            $query = "";
        }
        $lineNum ++;
    }
    # CONFIRM DATABASE HAS BEEN RESTORED AND THEN RESTART THE SCRIPT
    if ($lineNum < $_REQUEST["import"] + $linesThisSession) {
        echo ("<p class=\"success\">MySQL Database: $filename has been restored successfully.</p>\n");
    }
    if ($link)
        mysql_close();
    if ($file && ! $gzFile)
        fclose($file); else if ($file && $gzFile)
        gzclose($file);
} else {
    print"<p class=style2>You are not authorised to access this page</p>";
}
break;
?>

Link to comment
https://forums.phpfreaks.com/topic/151637-else-error/#findComment-796477
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.