I'm developing an api that can detect simultaneous request (For example 5 request is supported).
I have a sql table with rows limit and requests_made.
I have my attempt here the sleep(30) is just a delay for example.
<?php
include_once('connectdb.php');
$api_key_url = $_GET['api_key'];
$sql = "SELECT * FROM table WHERE api_key = '$api_key_url'";
$query = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($query)){
$account1 = $row['email'];
$requests_made= $row['requests_made'];
$limit = $row['limit'];
}
if ($check_speed > $check_limit){
echo"Your request couldn't be served. Your account is already running at maximum requests";
exit();
}
mysqli_query($conn, "UPDATE hash SET check_speed = requests_made + '1' WHERE email = '$account1';");
echo"Request Is Served";
sleep(30);
mysqli_query($conn, "UPDATE hash SET check_speed = requests_made - '1' WHERE email = '$account1';");
?>
This works perfectly until the user decides to exit the tab until it finishes loading because the counter will update +1 on the requests_made. Any suggestions for these ?
Use the ignore_user_abort function.
Related
i have a curious problem. When i make an update to a row, the info is updated, but not how i expect it to. i call this code
$count = $v_count+1;
mysqli_query($db_conn, "UPDATE videos SET v_count='$count' WHERE id='$vk'");
I have also tried this code as well...
if(isset($_POST['vk'])){
mysqli_query($db_conn, "UPDATE videos SET v_count='$v_count'+1 WHERE id='$vk'");
}else{
echo mysqli_error($db_conn);exit();
}
It does work, But the number is increased by 2, not the 1 i expected.This line HAS worked in other applications i have used... but now it is behaving oddly. Any help will be appreciated. Here is the full PHP block
$vk = $_GET['vk'];
if($vk != ''){
$sql = "SELECT * FROM videos WHERE id='$vk' LIMIT 1";
$query = mysqli_query($db_conn, $sql) or die (mysqli_error());
while ($row = mysqli_fetch_array($query)) {
$v_count = $row['v_count'];
}
mysqli_query($db_conn, "UPDATE videos SET v_count=v_count+1 WHERE id='$vk'");
mysqli_free_result($query);
}else{
header('location: index.php');
}
You probably have some error in your application logic such that $v_count in your PHP code doesn't equal the v_count field in your MySQL table.
If you use
mysqli_query($db_conn, "UPDATE videos SET v_count=v_count+1 WHERE id='$vk'");
it will always increase by 1 no matter what the value of your $v_count PHP variable.
I have a standard MySQL database, with around 60 rows (as in user accounts). When I first made it I made the mistake of making session IDs the same as the simple account ID, now I want to fix my mistake and I am obviously not going to go through 60 rows to reset them different secure session IDs, so I am writing this function:
function generate_sessionid(){
return bin2hex(openssl_random_pseudo_bytes(32));
}
function assign_all_sessionids(){
$sessionid = generate_sessionid();
$conn = sql_connect();
$result = mysqli_query($conn, "UPDATE accounts SET sessionid='$sessionid' WHERE 1");
sql_disconnect($conn);
}
assign_all_sessionids();
Problem: Every account in the database gets the same random session ID as the rest. How do I make it recall the function for each row in order to allow it to be random for each row?
Try get user's count from DB and simply execute it N times
function assign_all_sessionids(){
$conn = sql_connect();
// getting users count
// here just change 'id' to your id parameter
$result = mysqli_query($conn, "SELECT id FROM accounts");
$arr = $result->fetch_array(MYSQLI_NUM);
// executing N times
for($i = 0; $i < $result->num_rows; $i++){
$sessionid = generate_sessionid();
// here just change 'id' to your id parameter again
mysqli_query($conn, "UPDATE accounts SET sessionid='$sessionid' WHERE `id`=".$arr[$i]);
}
sql_disconnect($conn);
}
You can do what you want by first setting all the session ids to NULL:
UPDATE accounts
SET sessionid = NULL;
Then, inside the loop:
UPDATE accounts
SET sessionid = '$sessionid'
WHERE sessionid IS NOT NULL
LIMIT 1;
Normally you don't want to execute queries in a loop, however in this case you need to get all of the current unique identifiers, loop and generate a new identifier and then update one:
function assign_all_sessionids(){
$conn = mysqli_connect('whatever...');
$select = mysqli_query($conn, "SELECT sessionid FROM accounts");
while(list($id) = mysqli_fetch_assoc($select)) {
$sessionid = generate_sessionid();
$update = mysqli_query($conn, "UPDATE accounts SET sessionid='$sessionid' WHERE sessionid='$id'");
}
}
I have a database table with six records which are urls for six different shiny servers. There is a program that populates, on a real time basis, whether each of the servers are available. I have written a query that returns the url of the first available server. I have tested the script and determined the selection process works. I now want to perform a redirect to the available server using the "header" function and I am having difficulty determining the correct syntax. The URLs are in the format of "muscle.mysite.com:3535nameScan." Here is what I have at present.
$q = "SELECT url FROM ShinyServers WHERE Availability = '1' LIMIT 0, 1 ";
$r = mysqli_query($dbc, $q);
$row = mysqli_fetch_array($r, MYSQLI_ASSOC);
//printf ("%s\n", $row["url"]);
$url= printf ("%s\n", $row["url"]);
header ("Location: $url"); //redirect to muscle*
mysqli_close($dbc);
exit(); //before or after mysqli_close? I think after.
When I execute, I see mysite.com/40 in the address bar and I get a 404.
I tried this:
header ("Location: $row"); //redirect to muscle*
When I execute, I get "mysite.com/array in the address bar and I get my 404 page. I have tried many variations and I have thoroughly confused myself.
I hope you can see what I am trying to do. I have backed up to what does work which is
$q = "SELECT url FROM ShinyServers WHERE Availability = '1' LIMIT 0, 1 ";
$r = mysqli_query($dbc, $q);
$row = mysqli_fetch_array($r, MYSQLI_ASSOC);
printf ("%s\n", $row["url"]);
mysqli_close($dbc);
I get the first url that is available and I can see the results printed as muscle.mysite.com:3535nameScan. Now, I need to capture the results as a variable I can use in an UPDATE query and the header function. I have been searching for an answer and I have not found one as yet. I thought a different fetch command would be the answer but I could not find one that would apply to what I want to do.
I believe I am heading in the correct direction my using the suggestion to use "sprintf." However, I am still not able to "update" the table. Here is where I am at now.
error_reporting(E_ALL);
$q = "SELECT url FROM ShinyServers WHERE Availability = '1' LIMIT 0, 1 "; //make query
$r = mysqli_query($dbc, $q);
$row = mysqli_fetch_array($r, MYSQLI_ASSOC);
//printf ("%s\n", $row["url"]);
$url= sprintf ("%s\n", $row["url"]); //assign results to a variable?
echo "$url";
$qu = "UPDATE ShinyServers SET Availability = 0 WHERE url = $url LIMIT 1";
$ru = mysqli_query($dbc, $qu);
if (mysqli_affected_rows ($dbc) ==1){
echo '<p> The status has been updated</p>';
}else{
echo '<p class="error"> The status could not be updated</p>';
}
I get the expected results from 'echo "$url";' But I am getting the error message "The status could not be updated." I have been at this for so long I am afraid I am overlooking something. Is there a problem with the code or could there be a problem with the DB table? I looked at the DB table, created by someone else, and I noticed it does not have a unique column.
With prodigitalson and Dan08's help, I have a script that works. It is as follows
error_reporting(E_ALL);
$q = "SELECT url FROM ShinyServers WHERE Availability = '1' LIMIT 0, 1 "; //make query
$r = mysqli_query($dbc, $q);
$row = mysqli_fetch_array($r, MYSQLI_ASSOC);
//printf ("%s\n", $row["url"]);
$url= sprintf ("%s", $row["url"]); //assign results to a variable
//echo "$url";
$qu = "UPDATE ShinyServers SET Availability = 0 WHERE url = '$url' LIMIT 1";
$ru = mysqli_query($dbc, $qu);
if (mysqli_affected_rows ($dbc) ==1){
echo ' The status has been updated';
}else{
echo ' The status could not be updated';
}
mysqli_close($dbc);
header ("Location: http://$url");
exit();
My DB table is updated and I am redirected (I know I do not need the conditional statement). I learned also that the "header" function had to be after mysqli_close. I found that in the php manual. A little more tweaking and then I have the task of marrying this script into a registration script and a login script. Whoppee...thanks again.
This is my final script and it works perfectly
<?php
//selecting available server and update status
include_once '../DB/test1DB.php'; //connect to DB
error_reporting(E_ALL);
$q = "SELECT url FROM ShinyServers WHERE Availability = '0' LIMIT 0, 1 "; //make query
$r = mysqli_query($dbc, $q);
$row = mysqli_fetch_array($r, MYSQLI_ASSOC);
$url = sprintf("%s", $row["url"]); //assign results to a variable
$qu = "UPDATE ShinyServers SET Availability = 1 WHERE url = '$url' LIMIT 1";
mysqli_close($dbc);
header("Location: http://$url");
exit();
I am building a custom social network in php and mysqli. I have a page called notifications.php that shows the user site notifications as well as friend requests! put simply all I want to do is allow the user to purge his notifications list by clicking on a button!
in my html I have this...
<p><span id="purgeList"><?php echo $purgeList; ?></span></p>
<h2>Notifications</h2><?php echo $notification_list; ?></div>
and so far all I have for the button is this....
<?php
$purgeList = '<button disabled>Purge your List</button>';
if ($notification_list == true){
$purgeList = '';
}
?>
The notifications themselves gets pulled in using this script!
$notification_list = "";
$sql = "SELECT * FROM notifications WHERE username LIKE BINARY '$log_username' ORDER BY date_time DESC LIMIT 5";
$query = mysqli_query($db_conx, $sql);
$numrows = mysqli_num_rows($query);
if($numrows < 1){
$notification_list = "You do not have any notifications";
} else {
while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
$noteid = $row["id"];
$initiator = $row["initiator"];
$app = $row["app"];
$note = $row["note"];
$date_time = $row["date_time"];
$date_time = strftime("%b %d, %Y", strtotime($date_time));
$notification_list .= "<p><a href='user.php?u=$initiator'>$initiator</a> | $app<br />$note</p>";
}
}
mysqli_query($db_conx, "UPDATE users SET notescheck=now() WHERE username='$log_username' LIMIT 1");
If you are looking for a way to remove their notifications on the fly, you will need to do an XHR request (also known as AJAX)
for documentation on this you can visit jQuery API for more info on that.
you can cause them to reload the page to go to delete their notifications then use a header redirect to send them back to their 'news feed'
In my opinion i would use ajax because it does not cause a page reload and it can happen dynamically.
Hope this helps =)
I have a reminder mail sent to those who do not log on to my site after 30 days. Earlier I got an answer on this forum to create a seperate field and then update it as mentioned here: Need help on unix time stamp.
I have created a new field lastprelogin, now how do I update the respective field when I send the mail for inactive users.
<?php
include("include/data.php");
$query = "SELECT * FROM myusers WHERE DATE_ADD(FROM_UNIXTIME(lastprelogin), INTERVAL 15 DAY) < CURDATE()";
$result = mysql_query($query);
$num = mysql_numrows($result);
$i = 0;
while ($i < $num)
{
//send mail code
$sendmail = mail($to,$subject,$msg,$headers);
$i++;
if($sendmail) {
$query1 = "UPDATE myusers SET lastprelogin='".time()."'";
$result2 = mysql_query($query1);
}
}
?>
How can I update the respective user's lastprelogin field after sending the mail?
I am lost here beccause am unable to understand the logic in this part.
You need to loop through your results by using mysql_fetch_assoc or similar function.
Your update query needs to include the ID of the record you wish to update.
You should not use the mysql_* functions anymore as they are becoming deprecated soon. Use mysqli instead
<?php
include("include/data.php");
$query = "SELECT * FROM myusers WHERE DATE_ADD(FROM_UNIXTIME(lastprelogin), INTERVAL 15 DAY) < CURDATE()";
$result = mysql_query($query);
while ($user = mysql_fetch_assoc($result))
{
//send mail code
$sendmail = mail($user['email_address'],$subject,$msg,$headers);
$i++;
if($sendmail){
$query1 = "update myusers set lastprelogin='".time()."' WHERE id = " . $user['id'];
$result2 = mysql_query($query1);
}
}
?>
The logic of Your script is simple:
retrieve all the users that didn't log in for last 15 days
send an email to each user
and if that succeeds also update the field lastprelogin for that user
You have some important errors within Your script and this should be like this:
include("include/data.php");
$query = "SELECT * FROM myusers WHERE DATE_ADD(FROM_UNIXTIME(lastprelogin), INTERVAL 15 DAY) < CURDATE()";
$result = mysql_query($query);
while($user = mysql_fetch_assoc($result)) {
// assuming that myusers table has these columns: user_id, user_name, user_email, lastprelogin
//send mail code
if(mail($user['user_email'],'Please log in','Please login to my site',$headers)) {
$query1 = "update myusers set lastprelogin='".time()."' where user_id = {$user['usri_id']}";
$result2 = mysql_query($query1);
}
}
// end.
As a $headers variable You can set a From header, etc. Look for PHP mail function here: http://php.net/mail
Also the right query for updating should be this one:
"update myusers set lastprelogin='".time()."' where user_id = {$user['user_id']}"
anyway You will update the lastprelogin of all users everytime...
You will need to get some id from your myuser table and run the update query with where id = $id.