/    Sign up×
Community /Pin to ProfileBookmark

PHP takes empty captcha session

I’m a newbie in PHP and I have a question …
Is it possible in PHP?
I have a website with form + captcha on domain A -> there the captcha worked well.
It generates 1 session with the captcha code.
explanation -> https://postimg.cc/Wdzcw8vB
Now the server on which the domain A is hosted disabled sendmail – PHP mail() function
This way I can’t send forms anymore.
Since it’s impossible (Server hosting is already paid!) to move the website from domain A on server A to domain B on server B , I decided to use 2 domains on different servers.
I keep domain A on server A for the website and domain B on server B to send the forms -> works good except the captcha.
explanation -> https://postimg.cc/hhsRHvKb
My problem is when I call the form with captcha on domain B it generates **2 sessions** in the /temp folder of server B: one with the captcha code and one empty! Now It always choose the empty session so the captcha is always wrong.
What PHP code do I have to add so it takes the right session with the captcha code?
I hope someone can help me with my problem (if it’s possible of course)
Thanks in advance
Best regards.

to post a comment
PHP

15 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJan 26.2021 — I would just use something like phpMailer in the original PHP application to send the email via SMTP through whatever email server you choose to use (bypassing the need to use the local mail() function).
Copy linkTweet thisAlerts:
@coderunnerauthorJan 26.2021 — Thanks NogDog for your reply but phpMailer isn't supported either on server A ... mail function is disabled so nothing can be send. I already tried phpMailer. Doesn't work either.

Anybody else has a solution?
Copy linkTweet thisAlerts:
@NogDogJan 26.2021 — > @coderunner#1627202 I already tried phpMailer.

Did you use it in SMTP mode to use an external email server/account that supports SMTP (e.g. as in https://github.com/PHPMailer/PHPMailer/blob/master/examples/smtp.phps)?

Just want to make sure, before you even think about how to work around this issue (short of getting a refund from this hosting company and moving everything elsewhere :( ).
Copy linkTweet thisAlerts:
@coderunnerauthorJan 26.2021 — I added

ob_start();<br/>
session_start();<br/>
if ( !isset($_SESSION['captcha_code']) ) {<br/>
// does not exist yet, so create with 0<br/>
// you may want to initialize it to 1, thats up to you<br/>
$_SESSION['captcha_code'] = 0;<br/>
} else {<br/>
$_SESSION['captcha_code'] = $_SESSION['captcha_code'] == 0 ? 1 : 0;<br/>
}


but it doesn't always work in Chrome and does not work at all in Firefox.

Any ideas?
Copy linkTweet thisAlerts:
@NogDogJan 26.2021 — Yeah, browsers have been adding a lot of restrictions on passing cookies between domains. __EDIT__: I might be misinterpreting what you're doing in your last post, so take the preceding with a grain of salt.

My other thought is to create an API on your domain that does have mail capability, then when your app on the problem domain needs to send an email, send it via that API. (You might want to add security somewhere in that mix, maybe a secret token to include in the API request or such?
Copy linkTweet thisAlerts:
@coderunnerauthorJan 26.2021 — I'm just a newbie in PHP and you already talk about API. What' s that? 😅 *embarassed*

I found :above code while searching the internet ... and it worked 99% in Chrome and Edge but 0% in Firefox
Copy linkTweet thisAlerts:
@VITSUSAJan 27.2021 — @coderunner#1627212 You should learn about API which will help to do so many tasks easily.
Copy linkTweet thisAlerts:
@coderunnerauthorJan 27.2021 — I'll try to explain my problem ...

What I don't understand is ...

I open the domain A on server A with an iframe with contents which is on domain B on server B -> no session has been sent at all. Just opening a page from another server B in an iframe on server A.

Now from that page (normal page) which is on domain B on server B, I call a new page with form in the iframe with the captcha -> session_start(); -> so session is sent from domain B on server B

view explanation here -> https://postimg.cc/hhsRHvKb

So I call the session on domain B on server B -> why does it create 2 sessions instead of 1

Sorry for all my questions ... maybe my explanation is a little complicated but I try to understand all this.

For more info, just ask me!
Copy linkTweet thisAlerts:
@NogDogJan 27.2021 — That helps, some. Can you test by opening up what would be in the iframe directly in your browser, instead? (I.e. just open it in your browser using whatever link would be used for the IFrame element?) If so, do you still have the session problem, or is everything okay at that point?

If it works okay that way, then your issue is probably stuff browsers have in place to restrict cross-domain cookies. If you really need to use an IFrame (they're kind of obsolete these days and best avoided if possible), then I think you have to add some settings to the session cookies (`session_set_cookie_params(), I think?) to allow it -- and even then you may be thwarted by users who have chosen tighter security settings in their browser. See [this article](https://medium.com/trabe/cookies-and-iframes-f7cca58b3b9e) for some more info.
Copy linkTweet thisAlerts:
@coderunnerauthorJan 27.2021 — When I open the iframe separately (on server B) like you suggest then it works just fine.

**EDIT :** I think I better stop and try to find another solution (maybe a captcha without session) because nobody seems to have a solution ... IF there exists a solution for this problem of course.

**But you still may give possible solutions if you know one 😃**

A big "thank you" to NogDog!
Copy linkTweet thisAlerts:
@johncarryJan 28.2021 — Are you talking about APL.?
Copy linkTweet thisAlerts:
@coderunnerauthorJan 29.2021 — @johncarry

No about PHP.
Copy linkTweet thisAlerts:
@coderunnerauthorJan 31.2021 — So far no solution.

When I remove session_start(); on the page - then it works in all browsers - it generate only one session ( with the correct captcha value) but it still takes 3 or 4 captcha entries before the code is correct ... weird ...

Anybody an idea?
Copy linkTweet thisAlerts:
@coderunnerauthorFeb 14.2021 — Nobody knows a solution?

Regards
Copy linkTweet thisAlerts:
@coderunnerauthorFeb 17.2021 — It seems server related .... (settings)

Regards
×

Success!

Help @coderunner spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.21,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...