imgrooot
-
Posts
383 -
Joined
-
Last visited
-
Days Won
1
Posts posted by imgrooot
-
-
5 hours ago, ginerjm said:
Since nothing seems to be happening with this topic, let me ask this: Can you post the php code that is setting up your email headers for us to review? I basically want to see what the from address is set to since you never actually confirmed that for us.
Suere thing. Here is my code setup for sending emails.
$from_name = 'MyWebsite'; $from_email = '[email protected]'; $user_email = '[email protected]'; $post_subject = 'Hello World'; $post_message = 'It was a dark and stormy night...'; // Multiple recipients $send_to = $user_email; // Subject $subject = $post_subject; // Message $message = " <html> <body> <p>{$post_message}</p> </body> </html> "; // To send HTML mail, the Content-type header must be set $headers[] = 'MIME-Version: 1.0'; $headers[] = 'Content-type: text/html; charset=iso-8859-1'; // Additional headers $headers[] = 'From: '.$from_name.' <'.$from_email.'>'; // Mail it mail($send_to, $subject, $message, implode("\r\n", $headers));
-
Say I am running multiple contests at the same time. Each contest may have a different end date. One contest might run for a day, while the other one might run for 5 days. Below is the code done my way. I was wondering how you can improve it to make it more efficient?
$current_date = date('Y-m-d H:i:s'); $find_contest = $db->prepare("SELECT days_count, date_started FROM contests WHERE status = :status"); $find_contest->bindValue(':status', 1); $find_contest->execute(); $result_contest = $find_contest->fetchAll(PDO::FETCH_ASSOC); if(count($result_contest) > 0) { foreach($result_contest as $row) { $days_count = $row['days_count']; // NUMBER OF DAYS THE CONTEST IS RUNNING $date_started = $row['date_started']; // START DATE FOR THE CONTEST $date_1 = DateTime::createFromFormat('Y-m-d H:i:s',$date_started); $date_1->modify('+1 day'); $end_date_1 = $date_1->format('Y-m-d H:i:s'); $date_2 = DateTime::createFromFormat('Y-m-d H:i:s',$date_started); $date_2->modify('+2 days'); $end_date_2 = $date_2->format('Y-m-d H:i:s'); $date_3 = DateTime::createFromFormat('Y-m-d H:i:s',$date_started); $date_3->modify('+3 days'); $end_date_3 = $date_3->format('Y-m-d H:i:s'); if($days_count == 1) { if($current_date > $end_date_1 AND $current_date < $end_date_2) { $day = 1; $end_date = $end_date_1; require 'run-contest.php'; } } else if($days_count == 2) { if($current_date > $end_date_1 AND $current_date < $end_date_2) { $day = 1; $end_date = $end_date_1; require 'run-contest.php'; } else if($current_date > $end_date_2 AND $current_date < $end_date_3) { $day = 2; $end_date = $end_date_2; require 'run-contest.php'; } else {} } else {} } }
-
Say I have two variables like below. I basically want to select one of them, but through random choice. How do I do that?
$orange = 'Orange'; $apple = 'Apple';
-
Say I have an email [email protected] created in cPanel on a web hosting company.
I want to send email notifications to users on my website. I am using PHP Mailer to send the emails. But all the emails go directly into their junk box/spam folder, unless they confirm that [email protected] is not junk.
I am wondering if there is a work around for this?
-
On 1/27/2019 at 6:20 AM, Paul-D said:
Hi if i have $date = "12-08-2018" or "2018-08-12"
Need to subtract a month. Could this be done for day and year even?
TIA
Desmond.
P.S.
What I want to do is clear out a database table with records over a month old so today is 27th Jan 2018. I would like to create a date “2018-12-01”
I can then use this in an SQL to delete all before “2018-12-01”
Here's how I would do it.
// GET THE DATE FROM YOUR DATABASE TABLE $db_date = '12-08-2018'; // SUBTRACT DAYS FROM IT $get_date = DateTime::createFromFormat('Y-m-d H:i:s',$db_date); $get_date->modify('-30 days'); $new_date = $get_date->format('Y-m-d H:i:s'); // NEW DATE WILL BE YOUR ORIGINAL DATE MINUS 30 DAYS echo $new_date;
-
56 minutes ago, requinix said:
Then I slightly disagree with it. If all your files are within your document root then you might as well use DOCUMENT_ROOT, but if you have anything outside then you should use that constant.
Well one of the things the person mentioned was that SERVER['DOCUMENT_ROOT'] doesn't work on all servers and that DIR is better for PHP >= 5.3.
-
6 minutes ago, requinix said:
If it was someone on this forum then I'll agree with what they said.
PROJECT_ROOT instead of $project_root would be better. You don't need both of those anyways - either define() it or use a variable.
Got it.
And I actually read that on Stackoverflow.
-
On 1/25/2019 at 2:19 PM, benanamen said:
The better method is to have a single point of entry, eliminating duplicate code and not using the XSS vulnerable $_SERVER['PHP_SELF']
Could you please explain how using "$_SERVER['PHP_SELF']" makes it XSS vulnerable? What do I use it in it's place instead?
-
14 minutes ago, requinix said:
Try it.
The equals sign does not belong to the APP_PREFIX. It belongs to the <?. Documentation
You can't. The leading slash only works for URLs. If you want something that works for filenames then use
require_once $_SERVER['DOCUMENT_ROOT'] . '/core/init.php'; require_once $_SERVER['DOCUMENT_ROOT'] . '/members/dashboard.php';
or whatever the right path is. So DOCUMENT_ROOT + whatever the file path is relative to the root of your website.
I've read that not to use
$_SERVER['DOCUMENT_ROOT']
Instead use
define('PROJECT_ROOT', dirname(dirname(__FILE__))); $project_root = PROJECT_ROOT;
So using that, it would look like this.
require_once $project_root . '/core/init.php'; require_once $project_root . '/members/dashboard.php';
It seems to work fine now.
-
5 hours ago, kicken said:
Rather than absolute URL some people refer to these as domain relative URL's. Essentially when you begin the url with a / you are specifying the full path from the root domain name. If you want to link to localhost/mysite/index then you use <a href="/mysite/index">.
If you want the /mysite part to be changeable (say if you had an app you could drop into any folder on the site) then the easiest thing to do is to simply make it a configuration option in the application, for example:
config.php:
define('APP_PREFIX', '/mysite');
header.php:
<?php include('config.php'); ?> <link href="<?=APP_PREFIX?>/css/screen.css" media="screen" rel="stylesheet" /> <script src="<?=APP_REFIX?>/javascripts/jquery.js"></script>
Ah yes that's exactly what I was looking for. I have a couple questions.
1. <?=APP_PREFIX?>. Is this the same as "<?php =APP_PREFIX ?>" ?
2. What is the reason for the = sign in =APP_PREFIX?
3. I want to include it in the include/require files like this.
require_once '../core/init.php'; require_once '/members/dashboard.php';
How do I properly add the APP_PREFIX to the require_once above? I've tried different ways and i still get an error like this.
define('APP_PREFIX', '/mysite'); $root_dir = APP_PREFIX; require_once "$root_dir/core/init.php"; Warning: require_once(/mysite/core/init.php): failed to open stream: No such file or directory Fatal error: require_once(): Failed opening required '/mysite/core/init.php' (include_path='C:\xampp\php\PEAR')
-
2 hours ago, requinix said:
What I think is going on? You really don't want me to answer that question. Let's just say your solution is wrong.
"Absolute URL" here means a leading slash. That's it. That is all you... deep breath... All you need is the leading slash, like I showed in that post. Really.
You made this thread almost three days ago now. I gave you the answer in the first hour. There's no reason we should still be here.
I get what you're saying but your method of leading slash doesn't work. For e.g.
// CURRENT PAGE LOCATION: mysite/members/dashboard.php // YOUR METHOD <a href="/index">LINK</a> // RESULT localhost/index // WHAT I WANT IS localhost/mysite/index // I CAN ONLY GET THAT IF THE URL IS LIKE THIS <a href="../index">LINK</a>
Maybe I'm just not getting your leading slash. Please do explain what I am doing wrong.
-
On 1/25/2019 at 9:45 PM, kicken said:
@imgrooot, your example code doesn't show any need to generate an absolute URL, at least not dynamically. You'd just replace your URL's with absolute versions as demonstrated by requinix. The absolute URL's don't change based on the location of the script because you're always referencing from the root of the website rather than the current script's location.
If you think you need to somehow dynamically generate an absolute URL then you'll need to expand on why you think that is and the details of your situation.
Here is my setup for the absolute URL. The normal a links seem to work fine and so are the includes/requires. But the scripts in the HEAD section of the site don't seem to load, even though the path is correct.
define('PROJECT_ROOT', dirname(dirname(__FILE__))); $root_dir = PROJECT_ROOT; // THIS WOULD GET ME THE ROOT DIRECTORY OF MY SITE C:\xampp\htdocs\mysite // IF I INCLUDE IT IN THESE FILES <link href="<?php echo $root_dir; ?>/css/screen.css" media="screen" rel="stylesheet" /> <script src="<?php echo $root_dir; ?>/javascripts/jquery.js"></script> // I GET THIS ERROR IN THE INSPECT ELEMENTS WINDOW Not allowed to load local resource: file:///C:/xampp/htdocs/mysite/css/screen.css Not allowed to load local resource: file:///C:/xampp/htdocs/mysite/javascripts/jquery.js // IF USE RIGHT CLICK AND SEE THE PAGE SOURCE, THE PROJECT ROOT SLASHES ARE BACKWARDS LIKE THIS <link href="C:\xampp\htdocs\mysite/css/screen.css" media="screen" rel="stylesheet" /> <script src="C:\xampp\htdocs\mysite/javascripts/jquery.js"></script>
So what do you think is going on?
-
5 hours ago, requinix said:
Use absolute URLs
<link rel="shortcut icon" href="/images/favicon.ico.png">
<a href="/index">
<img src="/images/logo.PNG" alt="logo" />
I don't understand how so many people can be unaware of them.
I'm sorry but your post does not show how to get an absolute URL. I did some digging and found one of the posts here. https://stackoverflow.com/questions/6511496/how-to-generate-absolute-url-path-to-a-script-file
I will try the absolute URLs based on their examples.
-
Say I have two areas of a website.
1 - root directory (index, sign up, sign in)
2 - folders (this would contain folders such as snippets, members, assets)
Due to the nature of the folders, the links cannot be the same as they are on the pages in the root directory. Index page for example. So if I'm on members/dashboard.php, any a links linking to index.php, will have to have "../" in front of them. To solve the issue, my current set up is like this. But I understand it's not the most efficient way to do this. I was wondering if you can share your expertise for a better method.
$currentPage = basename($_SERVER['PHP_SELF'], ".php"); <?php if($currentPage == 'index' || $currentPage == 'members') { ?> <!DOCTYPE HTML> <head> <meta charset="UTF-8"> <title></title> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="description" content=""> <link rel="shortcut icon" href="images/favicon.ico.png"> <link href="css/screen.css" media="screen" rel="stylesheet" /> </head> <body> <a href="index"> <img src="images/logo.PNG" alt="logo" /> </a> </body> <?php } else { ?> <!DOCTYPE HTML> <head> <meta charset="UTF-8"> <title></title> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="description" content=""> <link rel="shortcut icon" href="../images/favicon.ico.png"> <link href="../css/screen.css" media="screen" rel="stylesheet" /> </head> <body> <a href="../index"> <img src="../images/logo.PNG" alt="logo" /> </a> </body> <?php } else { ?>
-
4 hours ago, requinix said:
If you need to work with cookies then stick with cookies.
Got it.
-
1 hour ago, requinix said:
$_SESSION is tied to the session cookie which may or may not last after the browser closes, depending on your session cookie settings. And for the most part sessions should end with the browser because that's a fairly standard practice. However it's quite possible to set the session cookie with an expiration of, say, a year, so it will survive after the browser closes.
So in your opinion, which method is better to use? The $_COOKIE method I used above or a $_SESSION one?
-
5 hours ago, ginerjm said:
Ok then. More like I thought.
So - to the OP - if cookies are your 'must-use' choice you have to realize that the cookie you set NOW is not available until the server is called upon to send a page to your client and only at that time will the cookies that exist AT THAT TIME be delivered to you for reference. If all you want is a user name, why not just set the SESSION array? Or even better just use the var that you create when you grab the url argument.
Because if I'm not mistaken, the SESSION only lasts until you close the browser. I want to use the COOKIES so that the referral name is saved for up to a year, unless someone deletes their browser cookies or uses a different referral link.
-
2 hours ago, requinix said:
setcookie( "ref", $url_ref_name, $date_of_expiry,"/"); $_COOKIE["ref"] = $url_ref_name;
Of course it was that simple. I should have thought of that. It works like a charm now.
Thanks again.
-
1 hour ago, requinix said:
$_COOKIE is only set when the cookies are being sent to the server. If you're telling the browser to create a new one it certainly couldn't have already been sent.
But there's nothing stopping you from adding to $_COOKIE. It's just an array, after all. You can add the value and pretend it was sent.
Not sure if I understand you correctly. But I have found a fix around. Instead of having the referral link go directly to the sign up page, I instead link it to the home page. Like this "www.mysite.com/?ref=johnsmith". This way when the user goes to the sign up page, the cookie will already be set. I tried it and it seems to work fine.
-
I want to show a cookie of a referral's username on a sign up page. The link is like this, www.mysite.com/signup?ref=johnsmith.
The cookie doesn't show if I go to that url page. But it does show up once I reload the page. So I'm wondering if it's possible to show the cookie the first time around, instead of reloading the page?
Here is my code.
// This is in the header $url_ref_name = (!empty($_GET['ref']) ? $_GET['ref'] : null); if(!empty($url_ref_name)) { $number_of_days = 365; $date_of_expiry = time() + 60 * 60 * 24 * $number_of_days; setcookie( "ref", $url_ref_name, $date_of_expiry,"/"); } else if(empty($url_ref_name)) { if(isset($_COOKIE['ref'])) { $user_cookie = $_COOKIE['ref']; } } else {} // This is for the sign up form if(isset($_COOKIE['ref'])) { $user_cookie = $_COOKIE['ref']; ?> <fieldset> <label>Referred By</label> <div id="ref-one"><span><?php if(!empty($user_cookie)){echo $user_cookie;} ?></span></div> <input type="hidden" name="ref" value="<?php if(!empty($user_cookie)){echo $user_cookie;} ?>" maxlength="20" placeholder="Referrer's username" readonly onfocus="this.removeAttribute('readonly');" /> </fieldset> <?php }
-
1 hour ago, requinix said:
Track down where in the code that exception is being thrown from (there should be a filename and line number), then look at what conditions it checks. It could very well be that gmp isn't really installed, or installed properly. Or the code is wrong.
I guess I really needed that sleep.
I have found the solution.
Apparently I didn't know how EasyApache 4 really worked. I had the package installed that contained the GMP extension. But The extension was not enabled. Once it was enabled, the package provision had to be reinstalled.
That was it. Now it works.
-
This is going to seem like a long shot. I've been trying to figure this out all night and still no luck. Perhaps someone more experienced can tell me why it's not working.
I have a CloudNX server with fasthosts.co.uk. I have installed a GMP extension on the WHM panel using EasyApache 4. Now I even tried adding "extension=gmp.so" in the php.ini file of both WHM and the cPanel where the website is located.
But no matter what, my site gives me an error like this. Fatal error: Uncaught exception 'Exception' with message 'GMP extension seems not to be installed' in
I am using this API https://block.io/api/simple/php . It requires GMP extension. It works great on my local server. I have no issue with GMP extension there.
So does anyone have a clue as to what I might be doing wrong and how I can fix this?
-
54 minutes ago, Barand said:
Yes
Perfect! Thanks a bunch!
-
1 hour ago, Barand said:
Almost - you are binding to :user_id when you used :name in the query.
You could speed it up significantly by doing a multiple insert EG
$params = []; $data = []; for($i = 1; $i <= $entries; $i++) { $params[] = '(?,?)'; array_push($data, $id, $name); } $stmt = $db->prepare("INSERT INTO entries(id, name) VALUES " . join(',', $params));; $stmt->execute($data);which creates a query in the form
INSERT INTO entries (id, name) VALUES (55, 'Smith'), (55, 'Smith'), (55, 'Smith'), ... , (55, 'Smith');Timings:
Yours : 0.3336 seconds Mine : 0.0591 seconds (5.6x faster)With hundreds of records it could be up to 50 or 60 times faster.
Oh wow, that's a much more efficient way of doing it.
My original query has more fields so i tried to simplify it for demonstration purposes. That's where I made the mistake of having :user_id when there shouldn't be.
Just one question. Your example shows inserting two fields(id, name). If I wanted to insert four fields, would it look like this?
$params[] = '(?,?,?,?)'; array_push($data, $id, $name, $field3, $field4);

Is there a way to simplify this code?
in PHP Coding Help
Posted
Yes this is what I was looking for. I'll give it a shot.
Thanks you.