str_pad(mt_rand(1,999999),06,'0',STR_PAD_LEFT Generating similar numbers - random

str_pad(mt_rand(1,999999),06,'0',STR_PAD_LEFT
I have had this code snippet on my WordPress site since 8 months ago. The purpose was to assign each user that fills the form (WPForm) a unique user ID. It has always worked well until today when I noticed that four entries have the same ID (One yesterday and Three today). I am running Astra Theme. Does anyone know what the course could be?

Related

Limit output length of crypt or hash

I was wondering if there is a way to limit the output of an encrypted or hashed value.
The case I have is, that I provide links for customers, having the id to an entry in my db, which contains relevant information for the receiver. Sooo... to avoid accessing to entries which are for someone else I am encrypting the id and append its outcome to the link. Now I am faced with the problem, that those "final links" are extremely long and ugly as f%!# (I actually got a lot responses, that they look highly suspicious and some of the customers didn't clicked on it, because they were afraid of being redirected to a phishing site).
However, this made me think about the option of limiting or individually setting the length of the encryptions outcome, like force it to contain 8 to 16 characters instead of about 250(? not sure how long they actually are). I also want to avoid using something like a redirecting page and a "self-made" URL shortener, because of the extra step I don't need.
Currently I've spent more than two hours of googling and reading several discussions regarding this topic and yeah... I am not satisfied with the results. Most of them started about two years up to five years ago.
What else I did?
I looked into Laravels api, especially into Illuminate\Encryption\Encrypter, but still no solution found.
Sooooo... I hope anyone can help me out with a solution based on laravel. I don't want to use anything else like php_mbcrypt itself than laravels encrypt or Hash::make.
Thanks in advance!

Purely Laravel: I would like to generate a key (token) on the page and then tell users to insert the key in a form in order to process the form

Who is asking? - Question is coming from less than 6 months old PHP developer who fell completely in love with PHP due to its awesomeness, also I just joined STACKOVERFLOW today 7th Dec, 2019.
Reason for the question: I have a form which I have completely built and validated with Laravel but I want to protect it from spam not with recaptcha but with a pin (a kind of generated key). I've seen it used on various websites and I also want to apply it.
Plan of action: The generated code will be placed at the end of the form with an input field and on filling it, it must match the code generated on every page refresh. If it doesn't match, I want to kill the page or perhaps, display a page with a "WELL DONE" message.
My thoughts: I'm new here and maybe the question might have been asked before, but honestly I've been on the computer for over a week (spending at least 18 hours searching and searching) but no really understandable solution.
What I can't do: Because I'm using Laravel, I don't know where to start this functionality and how to end it.
My helper: You are reading this and I believe you have the skills and techniques to help me without sweating at all. Just imagine a friend whose head is floating but the body is already in the ocean and about to drown. Also imagine a friend who has only one shot (2 days) to change his life, and if not done, only God knows what's to come. PLEASE HELP ME!
To everyone: Forgive me for the long message, I just believe that if I can express myself deeply enough, someone out there will help me out.
Thank you to all the awesome developers around the world.

Order posts by number manually entered by the user

I have a website which its admin user logs into a restricted area and adds content that is displayed on the public area. The admin wants a simple input text field on the post insert/edit form where he can type a number that determines in which position that post will be displayed to the public.
So, if the “Foo” post is given the number 3, it should be listed in the 3rd position, the old post 3 should become 4, 4 should become 5, and so on. If post 5 is repositioned to 1, 1 should automatically become 2, 2 should become 3, etc. If the admin reposition, 3 to 2, 2 should just become 3 without the need to change the other ones. If 10 becomes 9, 9 becomes 10 and no need to change other posts' position either. In short, when the position of one single post is changed (or inserted in the middle of existing posts), the remaining posts should have their position shifted accordingly.
I have considered two possibilities:
create a crazy algorithm that selects each post needed and updates the position column;
implement a linked list in the table so that each post always knows what is the primary key of the next one.
With the first option, when the admin inserts or updates a post's position, all the work is done at that moment, and when the public site is visited, the SQL just selects all the needed posts in a single query and order them by position. I don't really know yet how I would implement it, but I'm sure using a lot of code and possibly bad trade-offs it would be possible.
With the second option, there is much less work done during the insert/update, but when users access the site, I need to do one select for each post so that the right positioning can be achieved using the each post's link to the next “node”. So, if I have 150 posts in a given category, I'll have to run 150 selects (one for each post that is pointed to by the current "node". No pagination will be used in this case, and only the titles will be really listed, one below the other, so, I will really list them all.
I would like suggestions to other approaches, insights or improvements on the two possibilities I have described.
SOLUTION
I accepted #FractlizeR answer because it uses some other established piece of software to support his choice.
I wrote a small prototype on the solution I ended up using at work. Of course the code I used at work is different because I was changing a legacy system. Still, I used exactly the same SQL approach as the ones in the prototype. I hope I can improve that prototype and specially its comments soon, but at least it is there in case I or someone else needs to do something similar.
I would vote for #1. This is what, for example, XenForo forum engine does. It uses post position for paging and other things. When it needs to insert a new post into a thread, it queries thread for all posts, that needs to be reordered, then for each post calculates a new position and then updates table for all posts, which position changed.
We use XenForo for a long time in production and actually there should be no problems involved since this operation (reorder) is not so complex.

Time attendance algorithm

I've recently started to work on Time attendance software. People are using cards to check in and check out, but sometimes they check out before they check in and then some of them realize they made mistake and check in again. sometimes they check in instead of check out. I wrote an application that creates report and everything works fine when mistakes are simple, but sometimes people are just people and they check in for example 15 times.
I know my question is kinda complex and I doubt there is and answer but I was wondering if there is any algorithm which can determine such mistakes and can create decent report.
thanks in advance.
I think really if you are trying to have your software guess what the users intent was then you would need to base it on what the users schedule should be and what their expected check in/out cycle looks like
If its a workplace and the users are punching in their time and they work 8 hour shifts, you could try to be smart and flag checkins 7.5-8.5 hours apart as probably a check in that should have been a check out. Then you could flag back to back checkins 23+ hours apart as probably a missed check out on the previous shift. 16 hour differences would still probably be impossible to guess because they could be clocking out for a double, or changing their schedule and working an earlier shift the next day.
If this was for a college building you could probably at least say that back to back checkins that occur on separate calendar days were a missed checkout.

Is it possible to assign a work item in TFS to different people?

TFS (2008) has the great feature of work item tracking where I can easily see what people are doing all day long. Now I was wondering if I could assign a work item to different people or if they could write time on an item in a trackable way.
For example: We have two developers Mr. A and Ms. B. A did 4 hours of work and 50% of the work item "Create customer screen" until he gets ill. Than B has to finish the other 50% but I do not want to lose the progress of A because it could seem that A worked 4 hours less and B 4 hours too much.
Unfortunatly it seems that I can enter only one name in "assigned to" when I am using TFS 2008 and can not store the item if I try to seperate the names by comma or semicolon. Do you know if such a feature is included in TFS 2010?
Thank you for help.
No. This is one of the few aspects that haven't changed from 2008 to 2010.
Thomas
I'm not sure about assigning one item to multiple people but you could setup groups to which multiple people belonged. I'm not sure of your other requirements but this should solve this issue here. In essence Mr A and Mr B would both belong to a group called, say, 'Developers' to which the work item is assigned. Thus the full 8 hours is logged against a single entity.
Here is an (old) article on how to do this elegantly. You may want to split up your groups to as specific a category name as possible (e.g. 'Core Developers', 'Javascript Developers')
Found this link that implies that they are aware of the need but have not implemented a resolution yet
In TFS, if you assign a work item to someone else it will maintain that in the Work Item History, which is available for reports. TFS 2010, however, only tracks 3 fields: completed work (in hours usually), remaining work, and original estimate. If A and B both update completed work, you should be able to separate that work out in reporting services.
As #DarrellNorton said, all the information is recoded in the history of the work item, so you can retrieve the completed work values for each historical entry and correlate that to the assignee at that point in time. So the information you need is already in your database, if you can work out how to extract it. (The danger is that if someone leaves the completed-work field unchanged you might record the first dev's hours against the second dev as well - you'd ideally need to add a state transition rule in your work item templates that clears the field back to 0 whenever it was assigned to a new developer)
Another approach is to add your own fields to your TFS work items. It would be very easy to add (for example) fields "HoursDoneByMrA" and "HoursDoneByMrB" and expose these onto the work item form so that each developer could have independent statistics by which you could track the information you require. As long as your team size isn't huge, this would be quick/easy to achieve, and would also give you an instant summary on the work item itself of all developers who had touched the work item and their contributed hours, so you wouldn't even need to go as far as building a specialised report. (TFS PowerTools provides editors for the work item types that make adding and displaying this information much easier than hand-editing the XML templates. This approach would work in TFS 2005, 2008, 2010 - once you know how to use the power tools to do it, it would only take about a minute per developer to put this in place).

Resources