Jump to content

Help with php redirect


unknown1

Recommended Posts

I have created a script for my listing site and what the script does is

asks the user to add a line of code to his/her site to verify that they are the owner of the sites...

The script is working fine but I need some advice on how to rewrite it so that it only redirects after

both JavaScript alerts are completed or after all conditions have been met.

 

Thanks in advance!!

 

<?php
session_start(); 
include("config.php");

$sql_site = ("SELECT * FROM sit_details_tmp WHERE `email` = '{$_SESSION['EMAIL']}'");
$site_con = mysql_query($sql_site, $conn) or die(mysql_error());
$numrows = mysql_num_rows($site_con);

if($numrows > 0){
while($row = mysql_fetch_array($site_con))
{
           $sql_ver = ("SELECT Url FROM sit_details_tmp WHERE `email` = '{$_SESSION['EMAIL']}'");
   		   $site_ver = mysql_query($sql_ver, $conn) or die(mysql_error());
	   $rs_ver = mysql_fetch_array($site_ver);
	   
do {
$validationURL="$row[url]";
$htmlString=file_get_contents($validationURL);
$var_code = "$row[ver_code]";

if (eregi($var_code, $htmlString)) {

$ver_update=("UPDATE `sit_details_tmp` SET `id_verification` = '2' WHERE `email` = '$_SESSION[email]' AND `Url` = '$row[url]'") or die(mysql_error());

if (!mysql_query($ver_update,$conn))
  {
  die('Error: ' . mysql_error());
  }

echo '<script language=javascript>';
echo 'alert("Validation code was found! \n Your site has been successfully verified.");'; 
echo '</script>';
echo '<SCRIPT language="JavaScript">
<!--
window.location="my_account.php";
//-->
</SCRIPT>
';
}else{
echo"$var_code";
echo '<script language=javascript>';
echo 'alert("Validation code was not found! \n Please make sure you have entered the correct \n validation code and that its on the index page of your site.");';
echo '</script>';
echo '<SCRIPT language="JavaScript">
<!--
window.location="my_account.php";
//-->
</SCRIPT>
';
}
}while(mysql_fetch_array($site_ver));
}
}else{
echo"User not logged.";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/173192-help-with-php-redirect/
Share on other sites

I have created a script for my listing site and what the script does is

asks the user to add a line of code to his/her site to verify that they are the owner of the sites...

The script is working fine but I need some advice on how to rewrite it so that it only redirects after

both JavaScript alerts are completed or after all conditions have been met.

 

Thanks in advance!!

 

I have fix this issue and now notice that the script is sending out 2 alerts per test.

 

e.g.

Script checks 1st Url for verification code and then give message Validation code was found! \n Your site has been successfully verified. but it give the message twice

 

then the script check url two and find that the verification code is not found and send alert

Validation code was not found! \n Please make sure you have entered the correct \n validation code and that its on the index page of your site but also gives message twice.

 

Any ideas of how I can fix this... I'm sure once again I'm missing something easy.

 

 

 

<?php
session_start(); 
include("config.php");

$sql_site = ("SELECT * FROM sit_details_tmp WHERE `email` = '{$_SESSION['EMAIL']}'");
$site_con = mysql_query($sql_site, $conn) or die(mysql_error());
$numrows = mysql_num_rows($site_con);

if($numrows > 0){
while($row = mysql_fetch_array($site_con))
{
           $sql_ver = ("SELECT Url FROM sit_details_tmp WHERE `email` = '{$_SESSION['EMAIL']}'");
   		   $site_ver = mysql_query($sql_ver, $conn) or die(mysql_error());
	   $rs_ver = mysql_fetch_array($site_ver);
	   
do {
$validationURL="$row[url]";
$htmlString=file_get_contents($validationURL);
$var_code = "$row[ver_code]";

if (eregi($var_code, $htmlString)) {

$ver_update=("UPDATE `sit_details_tmp` SET `id_verification` = '2' WHERE `email` = '$_SESSION[email]' AND `Url` = '$row[url]'") or die(mysql_error());

if (!mysql_query($ver_update,$conn))
  {
  die('Error: ' . mysql_error());
  }

echo '<script language=javascript>';
echo 'alert("Validation code was found! \n Your site has been successfully verified.");'; 
echo '</script>';
echo '<SCRIPT language="JavaScript">
<!--
window.location="my_account.php";
//-->
</SCRIPT>
';
}else{
echo"$var_code";
echo '<script language=javascript>';
echo 'alert("Validation code was not found! \n Please make sure you have entered the correct \n validation code and that its on the index page of your site.");';
echo '</script>';
echo '<SCRIPT language="JavaScript">
<!--
window.location="my_account.php";
//-->
</SCRIPT>
';
}
}while(mysql_fetch_array($site_ver));
}
}else{
echo"User not logged.";
}
?>

First off, please don't bump. Secondly, you might try finding a way to do this in Javascript, as opposed to having to pass variables from Javascript to PHP (as PHP is server-side, and can't distinguish what's going on client-side. ie: what the user clicks).

First off, please don't bump. Secondly, you might try finding a way to do this in Javascript, as opposed to having to pass variables from Javascript to PHP (as PHP is server-side, and can't distinguish what's going on client-side. ie: what the user clicks).

 

Didn't bump... fixed the 1st issues I needed help with and found another.

Can you please elaborate on how I would go about writing the code in JavaScript...

Thanks!!

My apologies, I didn't read your post. I just saw that it was a quote, so I didn't expect you to write anything inside the quote.

 

About the Javascript... I really have no idea of the syntax, as I've never programmed any Javascript, ever. I don't really understand how it works, but with my limited coding knowledge, I'd guess something like this would work (obviously this is PHP):

 

function alertOne() { //Create function  alertOne() that declares variable $alertOne upon finishing.
//Code goes here.
    $alertOne = TRUE;
}

function alertTwo() { //Create function  alertTwo() that declares variable $alertTwo upon finishing.
    //Code goes here.
    $alertTwo = TRUE;
}

While (1) { //Always run this loop
    if (isset($alertOne)) && (isset($alertTwo)) { //If both alerts have been run, redirect.
        header( 'Location: http://www.example.com/index.html' ) ;
    } elseif ($conditionOne == 4) && ($conditionTwo == FALSE) && ($conditionThree == 'Red') { //If all condtitions have been met, redirect.
        header( 'Location: http://www.example.com/index.html' ) ;
    }
}

 

Obviously this would not work if it was hardcoded in PHP. This code would have to be ported over to Javascript (and even then I have no clue if it would work. As I said, I don't code JS).

 

 

Now, onto your next problem:

 

while($row = mysql_fetch_array($site_con)) {

The above line will execute pretty much all of your code for each row that it finds. So if you have two rows in the database with the same `email` field, this will repeat.

 

The next possible offender:

do { //Code } while (mysql_fetch_array($site_ver));

Again, the do-while could cause this code to execute itself multiple times. Is that what you're trying to do? Here's your current code (I cleaned it up a bit):

 

<?php
session_start(); 
include("config.php");

$sql_site = ("SELECT * FROM `sit_details_tmp` WHERE `email` = '" . $_SESSION['EMAIL'] . "'");
$site_con = mysql_query($sql_site, $conn) or die(mysql_error());
$numrows = mysql_num_rows($site_con);

if($numrows > 0){
while($row = mysql_fetch_array($site_con)) {
	$sql_ver = "SELECT `Url` FROM `sit_details_tmp` WHERE `email` = '" . $_SESSION['EMAIL'] "'";
	$site_ver = mysql_query($sql_ver, $conn) or die(mysql_error());

	do {
		$validationURL=$row[url];
		$htmlString=file_get_contents($validationURL);
		$var_code = $row[ver_code];

		if (eregi($var_code, $htmlString)) {
			$ver_update=("UPDATE `sit_details_tmp` SET `id_verification` = '2' WHERE `email` = '" . $_SESSION[email] . "' AND `Url` = '" . $row[url]" . '") or die(mysql_error());

			if (!mysql_query($ver_update,$conn)) {
				die('Error: ' . mysql_error());
			}

			echo '
				<script language=javascript>\n
				alert("Validation code was found!\n
				Your site has been successfully verified."); 
				</script>
				<SCRIPT language="JavaScript">
				<!--
				window.location="my_account.php";
				//-->
				</SCRIPT>
				';

		} else {

			echo $var_code . 
				'<script language=javascript>
				alert("Validation code was not found! \n Please make sure you have entered the correct \n validation code and that its on the index page of your site.");
				</script>
				<SCRIPT language="JavaScript">
				<!--
				window.location="my_account.php";
				//-->
				</SCRIPT>
				';
			}
	} while (mysql_fetch_array($site_ver));

} else {
	echo 'User not logged.';
}
}
?>

 

But if you wanted the code to only run once per query, you might try something similar to this:

 

<?php
session_start(); 
include("config.php");

$sql_site = ("SELECT * FROM `sit_details_tmp` WHERE `email` = '" . $_SESSION['EMAIL'] . "'");
$site_con = mysql_query($sql_site, $conn) or die(mysql_error());
$numrows = mysql_num_rows($site_con);

if($numrows > 0){
if ($row = mysql_fetch_array($site_con)) {
	$sql_ver = "SELECT `Url` FROM `sit_details_tmp` WHERE `email` = '" . $_SESSION['EMAIL'] "'";
	$site_ver = mysql_query($sql_ver, $conn) or die(mysql_error());

	if (mysql_fetch_array($site_ver)) {
		$validationURL=$row['Url'];
		$htmlString=file_get_contents($validationURL);
		$var_code = $row['ver_code'];

		if (eregi($var_code, $htmlString)) {
			$ver_update=("UPDATE `sit_details_tmp` SET `id_verification` = '2' WHERE `email` = '" . $_SESSION[email] . "' AND `Url` = '" . $row[url]" . '") or die(mysql_error());

			if (!mysql_query($ver_update,$conn)) {
				die('Error: ' . mysql_error());
			}

			echo '
				<script language=javascript>\n
				alert("Validation code was found!\n
				Your site has been successfully verified."); 
				</script>
				<SCRIPT language="JavaScript">
				<!--
				window.location="my_account.php";
				//-->
				</SCRIPT>
				';

		} else {

			echo $var_code . 
				'<script language=javascript>
				alert("Validation code was not found! \n Please make sure you have entered the correct \n validation code and that its on the index page of your site.");
				</script>
				<SCRIPT language="JavaScript">
				<!--
				window.location="my_account.php";
				//-->
				</SCRIPT>
				';
			}
	}

} else {
	echo 'User not logged.';
}
}
?>

 

Keep in mind that I didn't test either code, so they may contain typos, but nothing too hard to fix at all.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.