Pass array key to a function

i know that some of you dislike my methods but i’m happy with my homebrew framework and it works. so my framework and style aside (you can still insult me but also please help if possible) i am always struggling to fully understand arrays. I have a system of using session variables with post data to assign csrf tokens, random names and random values to my input fields. Everything works well except i am repeating code. I hate this and i would like to create one section of code to handle all of the unknown variables. I have my code working but it is only passing the value and not the name. I think that i am not able to see how this should work for some reason.

i hope that this makes sens. still, here is some code to clariify the matter.
my ‘router’ is just a section of code that checks if a session variable and the associated post variable exists or isset. if so, then set variables and pass to functions to check data, which is post name.value and token. If all is good, then show page, else relocate to index. All of my pages go through the index now via forms and not hyperlinks. my current code is like the following (which is obviously redundant):

if (isset($_POST[$_SESSION['router']['dashboard-buttonid']])) {
  $buttontext = $_SESSION['router']['dashboard-buttontext'];
  $buttonid = $_SESSION['router']['dashboard-buttonid'];
  $csrfsecret = $_SESSION['router']['dashboard-csrfsecret'];
  $csrfname = $_SESSION['router']['dashboard-csrfname'];
  $showpage = 'dashboard';
//repeat two more times for two other options besides dashboard page

//above code ends up here. so we are dealing with unknown session/post names (keys)
$buttoncheck = checkbutton($buttontext, $buttonid);
$tokencheck = checktoken($csrfsecret, $_POST[$csrfname]);
$_SESSION['getpage'] = $showpage;
header('Location: /');

i’m trying to set $_POST[$csrfname]) using the following code:

$testkey = ['dashboard-buttonid'];
foreach($testkey as $postkey) {
  if (array_key_exists($postkey, $_SESSION['router'])) {
    list($id) = explode('-', $postkey);
    $buttontext = ?;
    //what allows me to pass $_SESSION['router']['dashboard-buttontext'] and not its value?
    //so that i can still use $_POST[$csrfname] but as $_POST[$postkey]
    //as it stands, $_POST[$postkey] holds the random value
    //i want to pass the session variable along with the post and not its value
    $buttonid = ;
    $csrfsecret = ;
    $csrfname = ;
    $showpage = $id;

thus, to repeat myself:
$buttontext = ?;
what allows me to pass $_SESSION[‘router’][‘dashboard-buttontext’] and not its value?
so that i can still use $_POST[$csrfname] but as $_POST[$postkey]
as it stands, $_POST[$postkey] holds the random value
i want to pass the session variable along with the post and not its value

if i echo $postkey in the loop, then i get dashboard-csrfname, which is what i want.
i want to extract the name dahboard then use $postkey to reconstruct the unknown post:
$_POST[$_SESSION[‘router’][‘dashboard-csrfname’]] to pass this to a function.
my code currently passes the value of $_SESSION[‘router’][‘dashboard-csrfname’].
thus, my page is not loading (hence, header(‘Location: /’) enacted.)

any ideas how to pass the session variable and not its value?

No following very well. But as far as arrays go, you can get pretty deep in (try mongoDB with PHP and you really see how deep you can go!)

You can’t really pass an array key without a value, you can however get the key and ignore the value.

I need more details on what you want, before I can really help. This is also a time where I’ll say it could be an XYZ problem

i have tried everything to get this working so i’ll just let it go. i try to practice what i preach: “stick to your skill set”. i will just use my redundant code until i can learn better methods. as it stands, i cannot wait until i master php to get my site online. mastering will take years and i’m not waiting. classes and autoloaders are above me right now. my method works and i’ll have to stick with it for now. i only learn php in spare time. i am getting tired of working on this site and i’d like to wrap it up soon. i have too much on my plate right now. atleast i have it working and all of it goes through an index.

i’ll keep learning but for now, i will stick to my skill set of basic and redundant code. :slight_smile:

I can try and walk you thru it, but I need more info on what you are trying to rather than how you are trying to do it.

I got angry and solved the matter. I am very tired lately but this one really ticked me off. I couldn’t let it go. it was like kicking me off the network for bandwidth usage but i’m not done downloading. crack the knuckles and get back in :slight_smile:

my method might be novice and unprofessional but i got it to work by eliminating the variables and dealing directly with the session and post:

$testkey = ['dashboard-buttonid'];
foreach($testkey as $postkey) {
  if (array_key_exists($postkey, $_SESSION['router'])) {
    list($id) = explode('-', $postkey);
    $x = $id . '-buttonvalue'; $y = $id . '-csrfsecret'; $z = $id . '-csrfname';
   //then run the functions with x y z variables as session variable placeholders

Hey John, Hope you have been doing well. I, as well as others would be happy to teach you better methods, but if I remember correctly you were not willing step from your way of doing things nor show the application code for review even privately so I for one just stepped out of the conversation. If you get to where you will open up I will be happy to step in and get you over the humps so you can wrap this up as you expressed a desire to do.

On another note, thank you for your efforts in trying to help others that post to the forum.


Hello Sir Kevin, I am doing well and i hop that you are also doing well. I am very tired lately from working on this site but i also have a three year old son, a wife and two cats which keep me busy. I’ve spent three years cropping and editing my nature photos for this site. I have almost one million photos to sort through plus microscopic photos. The photos alone have destroyed any free time that i have. This site is becoming a huge burden. I am a novice and it hurts my productivity.

I have looked at frameworks a dozen times but i still don’t understand how to add my own custom pages and security. I gave up reading for productivity and i made a one session start, index.php framework but it is complex and frustrating. I am happy that this site is working but man is it a burden. I have alot to learn.

I made a github account awhile ago but i don’t know how it works. I have never used it before. I’ve only ever been on the client side browsing code. I certainly don’t have a problem with showing my code. I guess that i get sidetracked but i also figured that i will try to finish the site, then ask for opinions. I don’t know what is better.

Not to mention, i really am a beginner here. I’ve never owned and operated a website before so i read alot of things about frameworks and i move away from them because i don’t even know how to install it on a host or even if a host allows it. I have a host in mind but i want the site done before i ask them questions. I’ve never handled back-end stuff before. I’ve mastered html, css, js (dhtml) designs a long time ago. I never made the jump to back end programming until last year.

anyway, if you have some free time and don’t mind looking at my current code, then i can zip it up for you. let me know.

also, i am happy to help if i can do so. I know what it’s like to struggle with code, so i will do what i can to help out around here. I like to give back to the community :slight_smile:

Sure zip it up for me. As far as GitHub, there is an upload files button on your repo. That is the easiest way to get your files on there.

1 Like

no problem. i think that if you want to test my code, then you will also need my database code. i just use default root and no password while developing. I will need some time to put it all together for you. The time here is now 1am. i am about to log off for the night. i will start zipping in the morning. I can send you a pm when everything is ready.

Thank you very much for taking time to look over my code. I appreciate you.

Yes, give me everything needed to run it as you do. Get some rest. It’s a School night.

i’ve had a busy day today so i’m still trying to finish the router code before i zip everything. i don’t want to zip it with a half-coded router. this way, you can see the whole app. i will try to get a full zip by Sunday. please be patient. I’m working on it :slight_smile:

i actually finished this homebrew router and it’s pretty cool. you’ll see.

i’m trying to finish this today but you have to be patient. i’m changing all of my variable names and replacing my pages with skeleton code to make it easier (no photos or css effects). this is harder than i imagined it to be but no you can’t see my real variable names.

i hope to finished soon.

I’m making some notes for you in a text file because you or somebody else may be able to help me fix some code:

i want to stop multiple tabs for users at certain places in my site, such as, login, background changer, password change et cetera. I have a system working and it is nice but i’m missing something in my code. so i will isolate this code so that maybe someone can help me figure out what is the problem.

the problem is that whenever i return to the index page, i have to click twice to reneter the page, thus the session variable seems like it is not being set despite my attept to set it. so the first click must set it, then deny access because it is newly set. then once it is set, php continues. if i can solve this issue, then i can contain tabbed browsing of these pages.

anyway, i will separate the code in text file so that you can look it over and test it too.

sent a pm to you. i will upload the zip now.

This has piqued my interest. The reason being, it’s just a name, not the data itself.

1 Like

I prefix often with my trademark name, which is not yet a trademark

Honestly not worth doing this. Using that for a prefix name in tables is one thing, but doing it with the variable names is just a was of typing.

1 Like

well to quote you, “it’s just a name.” I don’t mid typing. To each his own. A name is not a style of programming, so criticism is really just opinion or personal preference. :slight_smile:

I should clarify: this is my website, i am the boss. so i don’t mind typing. However, if you are my boss and you tell me to not do this, then i do not do this. I just realized that impressionable minds may read my statements here, so i need to be clear. If you work for a company or you freelance (client is the boss), then do not use a prefix.

Usually your code is “prefixed” by your class namespace either way ^^

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service