Hi guys, could anyone help me to convert the following code into a php function? I need this code in every page and i thought maybe creating a function will save me a lot of time. Many thanks for your help
$user_email = $_SESSION['user_email'];
$query_subscription_master = mysqli_prepare ($connmaster, "SELECT master_user_membership_start, master_user_membership_end, master_membership_description, master_membership_file_quota, master_membership_group_quota FROM master_membership LEFT JOIN master_user ON master_user . master_user_membership = master_membership . master_membership_id WHERE master_user_email = ?");
mysqli_stmt_bind_param($query_subscription_master, 's', $user_email);
mysqli_stmt_execute($query_subscription_master);
mysqli_stmt_bind_result($query_subscription_master, $user_subscription_start, $user_subscription_end, $user_subscription_desc, $subscription_file_quota, $subscription_group_quota);
mysqli_stmt_fetch($query_subscription_master);
mysqli_stmt_close($query_subscription_master);
$today = date("Y-m-d H:i:s");
if ($today > $user_subscription_end){
// Rimando alla pagina subscription_expired se il file non esiste
header("location: subscription_expired.php");
}
@vincekaribusanaā¦since uāve mentioned that require this code in every pageā¦why dont u create this part as an individual php page and later include this page in other formsā¦
some.php
$user_email = $_SESSION['user_email'];
$query_subscription_master = mysqli_prepare ($connmaster, "SELECTmaster_user_membership_start, master_user_membership_end,master_membership_description, master_membership_file_quota, master_membership_group_quota FROM master_membership LEFT JOIN master_user ON master_user . master_user_membership = master_membership . master_membership_id WHERE master_user_email = ?");
mysqli_stmt_bind_param($query_subscription_master, 's', $user_email);
mysqli_stmt_execute($query_subscription_master);
mysqli_stmt_bind_result($query_subscription_master, $user_subscription_start,
$user_subscription_end,
$user_subscription_desc, $subscription_file_quota, $subscription_group_quota);
mysqli_stmt_fetch($query_subscription_master);
mysqli_stmt_close($query_subscription_master);
$today = date("Y-m-d H:i:s");
if ($today > $user_subscription_end){
// Rimando alla pagina subscription_expired se il file non esiste
header("location: subscription_expired.php");
}
The down side of including a bit of code like this is that you also have to consider the possibility of variable name clashes.
A function that returns either true or false depending on whether the subscription has expired would be a good thing to do. I would simply do the following:
Add a function declaration instead of the first line of code, something like
function checkSubscriptionCurrent($user_email, $dbconn) {
replace the header relocation code at the end with something like
return false;
else
return true;
}
Change every instance of $connmaster inside the function to be $dbconn because weāre passing that in as a parameter to the function.
and youāll get true if it is current, and false if it is not. Your calling code should then handle doing a header redirect if thatās what you want to do*. And your calling code should probably do some validation on the session variable before using it.
If youāre going to use the function a lot, you should separate that into a separate php file and include it in each of your other files that use it. For stuff thatās used a lot, I tend to just add them into the same file where I do the database connection, though there might be sound reasons for not doing that.
While youāre at it, you could optimise that query quite a bit. Within the function, you only use the $user_subscription_end variable, so thereās no point retrieving the others.
ETA - * you might not always want to do the redirect, hence leaving that out of the function.
I can then check if the subscription is stil valid using an if statement
$today = date("Y-m-d H:i:s");
if ($today > $end){
// send user to subscription_expired if subscription has expired
header("location: subscription_expired.php");
}
I donāt know if is possible to include the if statement into the function as well. Could you please tell me if this is a good code? And yes Iāll simplify the query removing the variables and tables I donāt need
-- returns an empty set if there is no match
SELECT
u.master_user_membership_start,
u.master_user_membership_end,
m.master_membership_description,
m.master_membership_file_quota,
m.master_membership_group_quota
FROM
master_membership m
LEFT JOIN
master_user u
ON
u.master_user_membership = m.master_membership_id
WHERE
u.master_user_email = ?
AND NOW() < u.master_user_membership_end
Hi @Dormilich thanks for your reply. I didināt think about inserting the if statement inside the MySQL query, that is a great idea should it not be NOW() > u.master_user_membership_end? Also how Cani redirect the user to a subscription page if membership has expired? Many thanks for your support
function check_membership($connmaster, $user_email){
$query_subscription_master = mysqli_prepare ($connmaster, "SELECT master_user_membership_start, master_user_membership_end, master_membership_description, master_membership_file_quota, master_membership_group_quota FROM master_membership LEFT JOIN master_user ON master_user . master_user_membership = master_membership . master_membership_id WHERE master_user_email = ? AND NOW() < master_user_membership_end");
mysqli_stmt_bind_param($query_subscription_master, 's', $user_email);
mysqli_stmt_execute($query_subscription_master);
mysqli_stmt_bind_result($query_subscription_master, $user_subscription_start, $user_subscription_end, $user_subscription_desc, $subscription_file_quota, $subscription_group_quota);
mysqli_stmt_store_result($query_subscription_master);
$numUsers = mysqli_stmt_num_rows($query_subscription_master);
mysqli_stmt_fetch($query_subscription_master);
if ($numUsers > 0) {
return array($user_subscription_desc, $user_subscription_start, $user_subscription_end);
}else {
header("location: ../administrator/subscription_expired.php");
exit();
}
}
Now the only problem is I get this error:
Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/MioCondominio/administrator/admin_dashboard.php:85) in /Applications/MAMP/htdocs/MioCondominio/includes/functions.php on line 38
Line 38 of functions.php is
header(ālocation: ā¦/administrator/subscription_expired.phpā);
Personally, Iād always have a function return something to the calling code and then act on it in the calling code, rather than having the function do the redirect. I (and again I stress itās a personal thing) dislike functions that have external limitations / requirements unless thereās no alternative - so here, you can only call this function before youāve sent any screen output, otherwise you get āheaders already sentā as you saw. Other similar things are functions that rely on global variables - the limitation being that you have to use the same names everwhere, or your function wonāt work - which is why I talked about passing the database connection as a parameter rather than just declaring it as a global.
But I do stress the āpersonallyā part of the above, again. If it works for you, then itās not really for me to pick holes in. I guess if your calling code will always do a header redirect under certain circumstances, then it does make some sense.