PNewCode
Members-
Posts
331 -
Joined
-
Last visited
Everything posted by PNewCode
-
Php and Laravel prevent duplicate entries help please
PNewCode replied to PNewCode's topic in PHP Coding Help
@Barand and @maxxd thank you for that info. Unfortunately I have no idea how to convert that to "Laravelese" as you said. Though that was a cool way of putting it haha. And yes the email is the username, because after getting a lot of grief from users in todays world of paranoid entries (and for good reason for many cases) I just changed it to entering a username instead of an email so that no email addresses are stored. There were already a few hundred pages that relied on it for other functions so I just left the column name in the database the same. I can't make heads or tales out of Laravel no matter how much I study it. It's like car mechanics to me... I can change the oil but rebuilding a motor is something my brain doesn't want me to know. Laravel, as it seems, is the motor. Spent the last month trying to comprehend this. No luck. I don't suppose either of you are for hire to make this function for me? -
Php and Laravel prevent duplicate entries help please
PNewCode replied to PNewCode's topic in PHP Coding Help
Thank you. The column name is "fname". Is that what you mean? In regular php I know how to check for duplicates and list them but thats as far as my knowledge goes. Laravel is a wild mess to me haha -
Hello everyone. I am wanting to prevent duplicate entries. This task is using laravel which I know little to nothing about. I'm putting this in PHP help because the file has php at the top of it. If this is the wrong place for this, then I apologize. The issue is that I googled for days and either I can't find the answer, or I can't translate what I find to fit what I need to do. The goal here is to prevent new users that sign up, to choose a username or screen name that already exists. I'm putting the code down below that I have to register the new users. The reason for this is because lately, I've had too many people replicating the same user names to make new accounts, either because they don't feel like asking for help when they forget the password or because they don't remember the answer to the question they created, or because they think making a new account gets them something new. So what I want to do is prevent duplicate user and screen names This laravel stuff is so far over my head that it's like trying to translate ancient egyptian cymbals for me. It took me over a week just to find where this file was haha Andy and all help is appreciated greatly NOTE: I didn't try to add any code to it yet because I'm incredibly nervous to destroy what is built because I don't understand this at all. <?php namespace App\Http\Controllers\Auth; use App\Models\User; use Illuminate\Http\Request; use Illuminate\Http\JsonResponse; use App\Http\Controllers\Controller; use App\Services\ImageUploadService; use Illuminate\Support\Facades\Hash; use Illuminate\Auth\Events\Registered; use App\Providers\RouteServiceProvider; use Illuminate\Support\Facades\Validator; use Illuminate\Foundation\Auth\RegistersUsers; class RegisterController extends Controller { /* |-------------------------------------------------------------------------- | Register Controller |-------------------------------------------------------------------------- | | This controller handles the registration of new users as well as their | validation and creation. By default this controller uses a trait to | provide this functionality without requiring any additional code. | */ use RegistersUsers; /** * Where to redirect users after registration. * * @var string */ protected $redirectTo = RouteServiceProvider::HOME; /** * Create a new controller instance. * * @return void */ public function __construct() { $this->middleware('guest'); } /** * Get a validator for an incoming registration request. * * @param array $data * @return \Illuminate\Contracts\Validation\Validator */ protected function validator(array $data) { return Validator::make($data, [ 'fname' => ['required', 'string', 'max:255', 'alpha'], 'username' => ['required', 'string', 'max:255', 'alpha_num'], //'lname' => ['required', 'string', 'max:255'], 'password' => ['required', 'string', 'min:8'], 'image' => ['image', 'mimes:jpg,jpeg,png','mimetypes:image/jpg,image/jpeg,image/png'], 'question' => ['required', 'string', 'max:255'], 'answer' => ['required', 'string', 'max:255'], 'ip_address' => ['required', 'string', 'max:255'], ]); } /** * Create a new user instance after a valid registration. * * @param array $data * @return \App\Models\User */ protected function create(array $data) { return User::create([ 'fname' => $data['fname'], //'lname' => $data['lname'], 'email' => $data['username'], 'question' => $data['question'], 'answer' => $data['answer'], 'password' => Hash::make($data['password']), 'unique_id' => rand(time(), 100000000), 'ip_address' => $data['ip_address'], // Save the user's IP address ]); } /** * Handle a registration request for the application. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse */ public function register(Request $request) { $this->validator($request->all())->validate(); $user = $this->create($request->all()); event(new Registered($user)); /*$filename = now().$request->file('image')->getClientOriginalName(); $request->image->storeAs('images',$filename,'public');*/ //$filename = (new ImageUploadService())->upload($request->file('image'), 'images'); //$user->update(['img'=>$filename]); $users = User::where('id','!=',$user->id)->get()->modelKeys(); foreach($users as $u){ $user->conversations()->create()->users()->attach($u); } $this->guard()->login($user); if ($response = $this->registered($request, $user)) { return $response; } return $request->wantsJson() ? new JsonResponse([], 201) : redirect('login.php'); //: redirect($this->redirectPath()); } }
-
Php escape string with GET function issue with apostrophe
PNewCode replied to PNewCode's topic in PHP Coding Help
@mac_gyver @jodunno @Barand I solved it. It took me following up with you all to realize what the issue was. It was in SENDING the value. So I made the adjustment below with what was suggested in here and it works perfectly In the URL I changed it to goup1=" . html_entity_decode(".$group22.".......... Good grief. I can't believe I spent over a week on this and thats all I had to do. Well, and make a better connection like you all stated too. Thank you all so much. I appreciate your help a LOT! -
Php escape string with GET function issue with apostrophe
PNewCode replied to PNewCode's topic in PHP Coding Help
@jodunnocourrect tourney is the table name. The reason why that information work for all the other values is because it's passing that information in the url. Below is an example url that works. The goupno and linkno are translated in this url to the database columns (in this example the columns are named group43 and link43) The reason I did this, is to have one page to use for all of the updates for 73 other entries. Below is a working example. Then I'll show the non working example working example (and all other entries work without an apostrophe) https://www.thewebsite/test.php?link1=https://www.youtube.com/watch?v=7WOR-Yj4fOk&group1=musicvideo - Dont be angry - YouTube&groupno=group43&linkno=link43 non working example https://www.thewebsite/test.php?link1=https://www.youtube.com/watch?v=7WOR-Yj4fOk&group1=musicvideo - Don't be angry - YouTube&groupno=group43&linkno=link43 the word "Don't be angry" shows on the page as "Don't be angry" however in this link it translates to the ' instead of the apostrophe. I'm not sure why since in the database, it says "Don't" correctly, but it's passing it with that translation. Then, when it gets to the next page (test.php) you get the code I gave above. But it only completes if there is no ' being sent -
Php escape string with GET function issue with apostrophe
PNewCode replied to PNewCode's topic in PHP Coding Help
@Barand I tried that just a few minutes ago too. And sadly no, that just brings me back to the original posts errors The charset I'm using is utf8mb4_unicode_ci now. I just updated the entire table to that from utf8mb3_general_ci -
Php escape string with GET function issue with apostrophe
PNewCode replied to PNewCode's topic in PHP Coding Help
@jodunno Thank you for that. I made those adjustments and I'm still getting an error. I should note that the groupno and linkno don't need to match the db because that is being translated in the url information. It works as long as there is no apostrophe in the values. Uncaught PDOException: SQLSTATE[HY000] [2019] Unknown character set in win1-2.php:17 Stack trace: #0 win1-2.php(17): PDO->__construct() #1 {main} thrown in win1-2.php on line 17 And line 17 is $pdo = new pdo("mysql:host=$DB_HOST;dbname=$DB_NAME;charset=$DB_ENCODING",$DB_USER,$DB_PASS,$options); -
Php escape string with GET function issue with apostrophe
PNewCode replied to PNewCode's topic in PHP Coding Help
Update @mac_gyver I missed changing the character set. So I did that and I get this Fatal error: Uncaught PDOException: SQLSTATE[HY000] [2019] Unknown character set in win1-2.php:18 Stack trace: #0 win1-2.php(18): PDO->__construct() #1 {main} thrown in win1-2.php on line 18 And line 18 is this $pdo = new pdo("mysql:host=$DB_HOST;dbname=$DB_NAME;charset=$DB_ENCODING",$DB_USER,$DB_PASS,$options); and I get this with any test to send without or with an apostrophe -
Php escape string with GET function issue with apostrophe
PNewCode replied to PNewCode's topic in PHP Coding Help
@mac_gyver You are clearly brilliant. So much in fact that I think my brain exploded trying to translate what you said to laymans terms haha. I didn't really understand your instruction, though I tempted to use what you posted with my own connection information and that didn't resolve the issue. Also made it so it doesn't even work without special characters now. I'm feeling certain it's because you explained how to use what you posted and I just don't understand what any of that means. But below is what I tried. Also it returned a new error (below) which doesn't happen when sending a word without an opostrophe Warning: Undefined array key "groupno" in win1-2.php on line 28 Warning: Undefined array key "linkno" in win1-2.php on line 29 <?php error_reporting(E_ALL); ini_set('display_errors', '1'); $DB_HOST = 'removed for posting'; // database host name or ip address $DB_USER = 'removed for posting'; // database username $DB_PASS = 'removed for posting'; // database password $DB_NAME = 'removed for posting'; // database name $DB_ENCODING = 'utf8mb4'; // db character encoding. set to match your database table's character set. note: utf8 is an alias of utf8mb3/utf8mb4 $options = [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, // set the error mode to exceptions. this is the default setting now in php8+ PDO::ATTR_EMULATE_PREPARES => false, // run real prepared queries PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC // set default fetch mode to assoc, so that you don't need to specify it in each fetch statement ]; $pdo = new pdo("mysql:host=$DB_HOST;dbname=$DB_NAME;charset=$DB_ENCODING",$DB_USER,$DB_PASS,$options); $group1 = $_GET['group1']; $link1 = $_GET['link1']; $groupno = $_GET['groupno']; $linkno = $_GET['linkno']; $sql = "UPDATE tourney SET $groupno = '$group1', $linkno='$link1' WHERE id=1"; ?> -
Php escape string with GET function issue with apostrophe
PNewCode posted a topic in PHP Coding Help
Hello. I have a tiny script that sends values to a database, which is grabbed from a different page that has values of the database. On the first page, there are a list of values that are from the db. Next to each line, there is a "send" button This button sends those valuse of that row through a url string to a new page The new page "Gets" those values and inserts them into a different place on the same database table (I don't know why, this doesn't make sense to me but I'm told it has to be this way and I can't change the structure of the db) It all works fine, except times when there's an apostrophe Here's a challenge I've been at for over a week now. I've managed to do this just fine with various escape strings when posting. But since this is GET, all of those methods don't seem to work Does anyone know a working method for this? Also, I can't change each entty manually because these entries could change at any time through the day by various users within a circle of friends. So I would have to watch it 24/7 to correct each one haha. Here is what I have and en example of the string url that gets sent to this is below it NOTE: The groupno and linkno are not a factor here as it works fine and it has a reason of it's own. It works fine when the information has douple quotes or other characters. Just not a single quote or apostrophe <?php error_reporting(E_ALL); ini_set('display_errors', '1'); $servername = "localhost"; $username = "removed for posting"; $password = "removed for posting"; $dbname = "removed for posting"; // Create connection $conn = mysqli_connect($servername, $username, $password, $dbname); // Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } $group1 = $_GET['group1']; $group1 = str_replace("'", "''", $group1); $link1 = $_GET['link1']; $groupno = $_GET['groupno']; $linkno = $_GET['linkno']; $sql = "UPDATE tourney SET $groupno = '$group1', $linkno='$link1' WHERE id=1"; if (mysqli_query($conn, $sql)) { echo " "; } else { echo "Error: " . $sql . "<br>" . mysqli_error($conn); } mysqli_close($conn); ?> Link Address example https://www.thewebsite/test.php?link1=https://www.youtube.com/watch?v=7WOR-Yj4fOk&group1=musicvideo - Don't be angry - YouTube&groupno=group43&linkno=link43 -
Hello everyone. I hope you're having a great day! I have a series of codes on my page that pic up different things when someone enters a youtube link in a form to send to the database. It works beautifully. I am now to where I need to Add to this to get a duration. The code below successfully gets the title. There is a lot more code on the page but it's not significant to this issue. What I need to do is get the duration and if it's more than 7 minutes long, then a message appears saying that the video is too long, try again. I've been searching for a couple weeks now when I get the time and all I can find is stuff to use with an api key, etc. I don't have that and I really don't want to alter a very long page (over 1000 lines) to adjust to that. So below is my code that gets the title. Is there a way to achieve the time duration limit from this as well? Thank you so very much The example below, if someone enters (aka this link is the $ytvideo1 value) https://www.youtube.com/watch?v=6ROwVrF0Ceg it will output (entered in database function is later in the page not showing below) Chuck Berry - Johnny B. Goode (Live 1958) $linkurl = "$ytvideo1"; parse_str( parse_url( $linkurl, PHP_URL_QUERY ), $vid ); preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:shorts/)?|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $linkurl, $match); $youtube_id = $match[1]; $preurl = "https://www.youtube.com/watch?v=$match[1]"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $preurl); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); $document = htmlspecialchars($output); curl_close($ch); $line = explode("\n", $document); $judul = ""; foreach($line as $strline){ preg_match('/\<title\>(.*?)\<\/title\>/s', $strline, $hasil); if (!isset($hasil[0]) || $hasil[0] == "") continue; $title = str_replace(array("<title>", "</title>"), "", $hasil[0]); }
-
@requinix Indeed! I spent a good part of the day digging in to understand it more, though I'm not totally getting it in my head yet but I understand a lot more now. Luckily, these profiles are on a completely seperate part of the website so the htaccess file only applies to that folder, and there's a different domain name that points to it. So it serves it's purpose. However as you said, if I restructure that then I'll have to revisit how I path the pages. Might end up having to use it by id's only
-
UPDATE: The following works. I'm posting it here in case it helps anyone else. I have to admit though that I don't understand WHY it works and I am going to dig deeper in this so I can understand. I give credit to @requinix because that reply did spark a few flares in this old noggin on some things to search for too Working htaccess code <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule (.*) /profile.php?id=$1 [L] </IfModule>
-
UPDATE: I changed the setting in PLESK that I found for the error so that's not an issue anymore. Now it's just a matter of it not working because the page isn't finding the chosen URL value extension
-
@requinix sorry I forgot to mention that. Yes I looked in the logs and the only error is how the htaccess file is affecting other pages with "Option FollowSymLinks not allowed here" but I'll tackle that headache later. I might have to do all of this in a different folder with its own htaccess to avoid all of that (example www.website.com/artist/john...) This is the newest one I have. No errors for it's own page but it's returning my own connection error I made when it can't find the specific name in the database so https://www.examplewebsite.com/profile.php?id=johndoe but https://www.examplewebsite.com/johndoe now just says "Which profile are you trying to see" (from not being able to grab the "custurl" value in the database) (side note: custurl is the row in the database where the value is entered for the chosen friendly url, in this case it being johndoe) ReWriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ profile.php?$1 [L,QSA]
-
Hello I've tried many many many different ways of making a rewrite htaccess file for this and every time I've come up with getting a 500 error. Even when used a bunch of the different generators to make them online. However even the generators don't really produce what I would want anyways, though I was using them to at least get a good starting point to figure it out. However no htaccess file will work as I keep getting the 500 error regardless This is what I'm trying to do. 1: There is a form on my website where people can type a single word with no spaces (and is forced lower case) to have a custom url 2: I want to make it so this url is what pulls up their page This link currently works https://www.examplewebsite.com/profile.php?id=johndoe (not a real link but for testing it works) And I am trying to get it to be so he can share his link to people just using https://www.examplewebsite.com/johndoe (much like you see for facebook pages and other friendly url website links) Alas, after 3 weeks and dried up eyes, I have had no luck at all. Is there a way to do this with just PHP? and if not, then is there a way to do this another way other than the htaccess rewrite file? Many thanks Note: I didn't include any of the htaccess codes that I tried because I tried so many of them that I don't even know which one would have been the closest to correct since they all gave me the same error
-
@gizmola You nailed it! And to @requinix I just realized from Gizmola's reply that I originally gave the wrong part of my code. I apologize for that. I thought the line that I provided was where my issue was. Apparently not. So for my education, if you don't mind... The reason Gizmola's worked is because it's the first part of a 3 section translation? First being domain, second being the (watch, si, etc) and then the last being the ID? Thats what it looks like to me now. I couldn't see that before. And looks as though they are separated by the " | " character? That really helped me a lot to better understand this. Thank you. Am I correct in my understanding of how it works?
-
I've been giving this a solid effort and still stuck. But I'm no further along since my last post. Any thoughts?
-
@requinix Here's the whole thing. It may not make sense (maybe?) because there's a lot of other stuff on the page (very long) that goes with everything involved for the user. But basically... the user enters a youtube link in a form. Then that form is sent to the (link-insert.php) which sends that link along with the title of the youtube link to the database (example: The link for the music video Korn - Life is peachy will send that title to the database) What I have originally works for shared links (youtu.be?si=) and regular links (youtube.com/watch?v=) and also songs from playlists. However if someone sends a link that is a short (youtube.com/shorts/VIDEO-ID-HERE) sends back a blank entry to the database as the title because it can't translate that link extension Note: $link is the field namd in the form $band is the column in the database The following also successfully allows the thumbnail to show on the page that calls the info from the database The shorts is the only thing that will send a blank entry to $band into the database ($band is the title of the video) You'll notice the last part, is the part I'm having the trouble with EDIT: Also, if it's not a youtube link at all, then "Not a youtube request" enters in $band in the database so that it's not blank $ytvideo1 = $link; $linkurl = "$ytvideo1"; parse_str( parse_url( $linkurl, PHP_URL_QUERY ), $vid ); preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $linkurl, $match); $youtube_id = $match[1]; $preurl = "https://www.youtube.com/watch?v=$match[1]"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $preurl); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); $document = htmlspecialchars($output); curl_close($ch); $line = explode("\n", $document); $judul = ""; foreach($line as $strline){ preg_match('/\<title\>(.*?)\<\/title\>/s', $strline, $hasil); if (!isset($hasil[0]) || $hasil[0] == "") continue; $title = str_replace(array("<title>", "</title>"), "", $hasil[0]); } $validateurl = $link; $regex_pattern = "/(youtube.com|youtu.be)\/(watch)?(\?v=)?(\S+)?/"; $match; if(!preg_match($regex_pattern, $validateurl, $match)){ $band = "Not A Youtube Request"; }else{ $band = $title;; }
-
@requinix Thank you for that. I tried the following but with no luck, based on what you said. Admittingly, I have to come to best guess solution for it because regex is the bane of my brain haha $regex_pattern = "/(youtube.com/shorts|youtube.com|youtu.be)\/(watch)?(\?v=)?(\S+)?/"; $regex_pattern = "/youtube.com/shorts/(\w+)|youtube.com/watch\?v=(\w+)|youtu.be/(\S+)?/"; $regex_pattern = "/youtube.com/shorts/(\w+)|youtube.com/watch\?v=(\w+)|youtu.be/?si=(\w+)/"; $regex_pattern = "/youtube.com/shorts/(\w+)|youtube.com/watch\?v=(\w+)|youtu.be/?(\S+)/"; I should also note that the following is my original one that works for all but the shorts but I tried to use it with variations of what you said and no luck... $regex_pattern = "/(youtube.com|youtu.be)\/(watch)?(\?v=)?(\S+)?/"; Edit: No, I don't want to use javascript. I just added that in the original post because others I have asked keep telling me to use javascript instead haha
-
Hello. I have the following code that works for it's intended purpose for all youtube links including shares (si) extensions. Can someone please tell me how to alter it to add the shorts? This is in PHP and has to remain that way. I can't use javascript for this because this is part of a very long php coded page. Shorts example https://www.youtube.com/shorts/J0iIQ629N2c My code that works for all but shorts $regex_pattern = "/(youtube.com|youtu.be)\/(watch)?(\?v=)?(\S+)?/"; I have tried the following and it failed. I should note that I have been trying to understand regex patterns and my mind doesn't seem to want to learn it at a normal good rate but I keep trying $regex_pattern = "/(youtube.com|youtu.be)\/(shorts|watch)?(\?v=)?(\S+)?/"; $regex_pattern = "/(youtube.com|youtu.be)\/(shorts)\/(watch)?(\?v=)?(\S+)?/"; $regex_pattern = "/(youtube.com|youtu.be)\/(watch)?(\?v=)?(\S+)?(\shorts)?/";
-
@Barand Thank you I will try to put that in with what @mac_gyver gave too. I've been studying this a lot over the last couple days and I'm starting to understand how it all works. I'm still stuck on how to keep a new question from showing (or the current question) below the results (after the user answers it). I unset it all to get it to do a new one in case of refresh so that works but even before I did that then it would still show that question again below the results. The problem with that is that it will allow the user to answer again if they get it wrong. If they get it wrong then there's no redo, I don't want them to have the ability to put in a new answer to move forward.
-
@mac_gyver I resolved the division zero issue by changing the lines rand(0, 9) to rand(1, 9) If you don't mind an extenstion on it, is there a way to not show the question and answer form after the answer is given? Right now, it shows the answer again under the CORRECT or INCORRECT display, after the answer is given.
-
@mac_gyver To answer your question, the goal was for learning and also give my viewers a simple math question that will go along with a bunch of other trivia. Just for fun. And yes also to learn from. I'm told by most people that I know that my mind works weird. I tend to reverse engineer things to learn most. I'll take a web page and see it as a viewer to see what it does, then look at the script to see how it works, then learn and build from there. Most of the time it works for me, but then other times it doesn't. Unfortunately I have a learning handicap to where if I was to be given instructions on how to build a clock (like a literal one that is on the wall) I would be lost. But if I took a clock apart, then I can see how it's done and build another one. And then often enough I can build more to add to that clock from there. I know... I'm strange. That being said, what you posted is VERY educational to me! I simply pasted your code that you provided into a page and then uploaded it. I saw it working. Then I went back to go line by line from what you said before providing the code and learned from it while going through that code. And now it all MOSTLY makes sense to me. I confess that not all of it does but I know it will soon, as some of it is just termonology that I'm unfamiliar with. HOWEVER, it has some terms that I didn't understand 2 or 3 months ago, but now do because of this website. Overall, I understand a lot more of why it works as apposed to what I had created to begin with. Sorry this is a long post but I wanted to properly thank you by explaining exactly how it educated me. I am very greatful for that! THANK YOU!
-
I hope I'm not stepping out of bounds on this, but is there a way I can just pay someone to make this work? If I'm out of line then I appologize. Another 5 hours have passed and I can't make heads or tails out of how to make this work. I don't need to save the answer in a database or anything like that. It's just going to be as basic as can be. A random math question shows, the user enters the answer, then it shows if it's right or wrong without showing a new math question. I feel foolish I can't find a single thing online to where I can learn what to do here. And I'm sure you both are correct in what you're saying. Unfortunately, I don't understand what you're saying